How to write XML with any name and type?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
One option would be to:

1. Create a class that implements the ISerializable interface
2. Serialize the class to XML (sample code below)
3. Deserialize the class from the XML back into a class (no code sample
on that one - sorry)

Sample Code

using System.Xml.Serialization;
using System.IO;

protected string SerializeObjectToString(object data)
{
if (data == null)
return string.Empty;

if (data.GetType().Name == "String")
return (string)data;
else
{
XmlSerializer mySerializer = new
XmlSerializer(data.GetType());

StringWriter myWriter = new StringWriter();
mySerializer.Serialize(myWriter, data);
string ret = myWriter.ToString();

myWriter.Close();

return ret;
}
}

Bill
 
One option would be to:

1. Create a class that implements the ISerializable interface
2. Serialize the class to XML (sample code below)
3. Deserialize the class from the XML back into a class (no code sample
on that one - sorry)

Sample Code

using System.Xml.Serialization;
using System.IO;

protected string SerializeObjectToString(object data)
{
if (data == null)
return string.Empty;

if (data.GetType().Name == "String")
return (string)data;
else
{
XmlSerializer mySerializer = new
XmlSerializer(data.GetType());

StringWriter myWriter = new StringWriter();
mySerializer.Serialize(myWriter, data);
string ret = myWriter.ToString();

myWriter.Close();

return ret;
}
}

Bill
 
Thanks Bill for you replay.

I'm not sure I understand how the serialization and deserialization can
solve my problem.

The XML file may contain any tree structure the user wishes, and I will now
this tree on at run time.

So, at run time, I need to reed the entire parameters from the XML, and
while reading I need to reed the parameters value without doing any code
switching and casting. I want to read the value in the correct type.

Can you please explain if and how your suggestion of serialize and
deserialize can do all of that?
 
Do you know the full list of possible parameters up front? If so, you
could build a serializable "parameters" class that contains a value for
all of the parameters. As you read the XML you could populate the
appropriate parameter (and have it strongly-typed).

i.e.

[ISerializable]
public class MyParameters
{
public int Property1 { get; set; }

public string Property2 {get; set; }
}

If you don't know the full list of possible parameters beforehand, how
are you validating the incoming XML - is there an XSD? If so, you
could use the xsd.exe included in the .NET tools to create a class for
that schema that can directly read in the XML. This would allow you to
manipulate the XML in code. For more info see:
http://msdn.microsoft.com/library/d...s/html/cpconxmlschemadefinitiontoolxsdexe.asp.

Bill
 
No, sorry, I do not know full list of possible parameters up front.

And I can not use XSD, because the the XSD schema need to now beforehand the
tree structure and parameters names and types, which I do not. Only at run
time I now all of that.
 
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});
}
}
 
Back
Top