all groups > dotnet xml > october 2006 >
You're in the

dotnet xml

group:

How to write XML with any name and type?


How to write XML with any name and type? Demorsy
10/30/2006 7:20:01 AM
dotnet xml:
Disappointed, this is not the first time I'm posting this kind of a request
for help, but no helpful answer was posted yet.

I'll try make my question as clear as I can:

(1) I wish to write an XML file. this file will contain any number of
parameters to be used in my C# code as database.
(2) This file may contain any number of parameters, and I will know the
number of the parameters only at run time.
(3) Any parameter must have a name, but this name may be any name, and I
will know the names of the parameters only at run time.
(4) Any parameter must have a type, this type can be of any well known type
(int, string, float, dateTime, byte etc.), and I will know these types only
at run time.
(5) Any parameter may or may not have several more attributes
(authorization, precision, range), I will now if any of this attributes exist
only at run time.
(6) Any parameter will have a value of the type as described above.

I need to read this parameters form the XML file as their real value without
doing any code switching, and to assign these values to my run time data
members.

I thought I'll be able to load the XML file into DataSet and read the
parameters from it. But I'll be happy to use any other solution you may have.



---------
Thanks
RE: How to write XML with any name and type? Sharon
11/2/2006 6:42:01 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
AddThis Social Bookmark Button