Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to write XML with any name and type?

From: SharonG@----------.------
To: NULL
Date: 11/2/2006 6:41:00 AM
Ok, I think I found a way to do thanks to Marc Clifton articale on 
CodeProject (http://www.codeproject.com/dotnet/MycroXaml.asp).

The XML file can be somthing like that:

<?xml version="1.0" encoding="utf-8"?>
<System Name="System">
    <Paramaters>
  <Speed type="System.Iint32">111</Speed >
  <_xAxis type="System.Single">222</_xAxis>
  <_yAxis type="System.Double">333</_yAxis>
  <_ID type="System.Int64">444</_ID>
  <_Name type="System.String">String value...</_Name>
    </Paramaters>
</System>


And the C# code can be:

System.IO.StreamReader sr = new System.IO.StreamReader("TypesDef.xml");
string text = sr.ReadToEnd();
sr.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(text);
XmlNode node = doc.FirstChild;
while( node.NodeType.ToString() != "Element" )
{
   node = node.NextSibling;
}

XmlNode paramsNode = doc.SelectSingleNode("//Paramaters");
XmlNodeList nodeList = paramsNode.ChildNodes;
Hashtable verTypes = new Hashtable(nodeList.Count);
string typeName = "";
Type type;

foreach( XmlNode xmlNode in nodeList )
{ 
   XmlAttribute attr = xmlNode.Attributes["type"];
   typeName = attr.Value;
   if( typeName == "System.String" )
   {
      verTypes[xmlNode.Name] = xmlNode.InnerText;
   }
   else
   {
      type = Type.GetType(typeName, false);
      object typeIntance = Activator.CreateInstance(type);
      System.Reflection.MethodInfo Parse = type.GetMethod("Parse", new Type 
[] {typeof(String)});
      verTypes[xmlNode.Name] = Parse.Invoke(type, new object[] 
{xmlNode.InnerText});
   }
}

------
Regards
Sharon


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent