Consider the following serializable classes:
class Item {...}
class Items : List<Item> {...}
class MyClass
{
public string Name {get;set;}
public Items MyItems {get;set;}
}
I want the serialized output to look like:
<MyClass>
<Name>string</Name>
<ItemValues>
<ItemValue></ItemValue>
<ItemValue></ItemValue>
<ItemValue></ItemValue>
</ItemValues>
</MyClass>
Notice the element names ItemValues and ItemValue doesn't match the class names Item and Items, assuming I can't change the Item or Items class, is there any why to specify the element names I want, by modifying the MyClass Class?
As with the CreatePo method, you must first construct an XmlSerializer, passing the type of the class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.
XmlRootAttribute Class (System.Xml.Serialization) Controls XML serialization of the attribute target as an XML root element.
Xml. Serialization namespace) class is used to serialize and deserialize. The class method Serialize is called. Since we have to serialize in a file, we create a " TextWriter ".
The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.
public class MyClass
{
public string Name {get;set;}
[XmlArray("ItemValues")]
[XmlArrayItem("ItemValue")]
public Items MyItems {get;set;}
}
You might want to look at "How to: Specify an Alternate Element Name for an XML Stream"
That article discusses using the XmlElementAttribute
's ElementName
to accomplish this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With