Hi George,
Thanks for posting in the community!
From your description, you need some suggestions or information on how to
specify the proper XmlAttributes in certian classes so as to use
XmlSerializer to output them and the certain custom class you made contains
an array member of another custom class, yes?
If there is anything I misunderstood, please feel free to let me know.
As for this question, I suggest that you use the "XmlArray" and
XmlArrayItem" attribute to implement the control of the array member within
a certain class's serialization. For example, we can provide the class's
code as below:
public class clsLine
{
public clsLine()
{}
public string Name = "name";
public string Description = "description";
}
public class clsOrder
{
public clsOrder()
{}
[XmlElement("MyHeader")]
public string Header = "header";
[XmlArrayItem(typeof(clsLine),ElementName = "MyLine")]
[XmlArray(ElementName = "MyLines",Namespace = "", IsNullable = true)]
public clsLine[] Lines = null;
}
Notice the two attributes specified in the clsOrder class.
1. the "XmlArray" has specified what name to use for the array member when
the class's instance is serialized, here I specify it as "MyLines", and the
"XmlArrayItem" can be used to specify the array member's item's serialize
options, and when i serialzie a certain clsOrder instance via the following
code:
----------------------------------------------------
XmlSerializer serializer = new XmlSerializer(typeof(clsOrder));
clsOrder odr = new clsOrder();
odr.Header = "Test Order";
clsLine[] lines = new clsLine[5];
for(int i=0;i<lines.Length;i++)
{
clsLine line = new clsLine();
line.Name = "Name" + i.ToString();
line.Description = "Description" + i.ToString();
lines
= line;
}
odr.Lines = lines;
TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,odr);
writer.Close();
-------------------------------------
The output.xml is like:
-------------------------------output.xml-------------------------
<?xml version="1.0" encoding="utf-8"?>
<clsOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyHeader>Test Order</MyHeader>
<MyLines>
<MyLine>
<Name>Name0</Name>
<Description>Description0</Description>
</MyLine>
<MyLine>
<Name>Name1</Name>
<Description>Description1</Description>
</MyLine>
<MyLine>
<Name>Name2</Name>
<Description>Description2</Description>
</MyLine>
<MyLine>
<Name>Name3</Name>
<Description>Description3</Description>
</MyLine>
<MyLine>
<Name>Name4</Name>
<Description>Description4</Description>
</MyLine>
</MyLines>
</clsOrder>
----------------------------------------------------
In addition, here is some tech references in MSDN on the how to use and
control the XmlSerializer, I believe they'll be helpful to you:
#Introducing XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconintroducingxmlseri
alization.asp?frame=true
#Examples of XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconanexampleofxmlseri
alizationwithxmlserializer.asp?frame=true
#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrollingseriali
zationbyxmlserializerwithattributes.asp?frame=true
#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconattributesthatcont
rolserialization.asp?frame=true
Please check out my preceding suggestions, if you have any questions on
them, please feel free to let me know.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)