c# deserialize xml with a specified 'encoding' and 'stylesheet' lines
I am creating an XML file using the System.Xml.Serialization module. I have a class that gets serialized into an XML file. The file looks like this:
<itemList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm开发者_StackOverflow社区lns:xsd="http://www.w3.org/2001/XMLSchema">
<itemListed>
<item ID="81288" Synopsys="Reset search point" CompletedTime="7/27/10 4:12 PM" Resolver="owner1" />
<item ID="81285" Synopsys="Added contructor" CompletedTime="6/05/10 9:23 AM" Resolver="owner2" />
</itemListed>
</itemList>
Problem is, I would like it to generate this:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="item.xsl"?>
<itemListed>
<item ID="81288" Synopsys="Reset search point" CompletedTime="7/27/10 4:12 PM" Resolver="owner1" />
<item ID="81285" Synopsys="Added contructor" CompletedTime="6/05/10 9:23 AM" Resolver="owner2" />
</itemListed>
Any idea what I need to change to my class?
My code:
public class Item
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("Synopsys")]
public string Synopsys { get; set; }
[XmlAttribute("CompletedTime")]
public string CompletedTime { get; set; }
[XmlAttribute("Resolver")]
public string Resolver { get; set; }
}
public class ItemList
{
[XmlArray(ElementName = "itemListed")]
[XmlArrayItem(ElementName = "item")]
public List<Item> ItemList { get; set; }
}
I appreciate any help. Thanks Tony
I haven't found an easy way to customize the stylesheet or the encoding line but I found a good suggestion here:
link text
The idea is to pretty much write your own serialization class. I took the idea from the article and I created a class that serializes the class (using the C# library) then a filter modifies the header to modify the encoding line and add the stylesheet line.
When I load the xml, I read the file, I pass it through the filter to remove the stylesheet line and I change back the encoding line. Once I have done that, I use the de-serializer provided by C#.
It seems to work.
Tony
One solution might be to implement IXmlSerializable interface on your class. I'm not sure if the XmlWriter will allow you to write a XML tag or not. Proper way to implement IXmlSerializable?
精彩评论