问题
Here's the XML:
<?xml version="1.0" encoding="utf-8" ?>
<SAPPHIRE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TRANSACTION-CODE>NEW</TRANSACTION-CODE>
<CUSTOMER-NUMBER>100398598</CUSTOMER-NUMBER>
<CUSTOMER-NAME>CART DUDE</CUSTOMER-NAME>
<ACCOUNT-TYPE />
<PERSON FNAME="CART" LNAME="DUDE" RESPONSIBLITY="CART DUDE" />
<SOURCE>cplestore</SOURCE>
<TRAN-REFERENCE>13374470</TRAN-REFERENCE>
<ORDER>
<ORDER-NUMBER NUMBER="00241662693" REFERENCE="13374470">
<PRODUCT-CODE>DLP99022L</PRODUCT-CODE>
<START-DATE>2011-4-6 00:00:00.0</START-DATE>
<EXPIRE-DATE>2011-4-11 00:00:00.0</EXPIRE-DATE>
<MAX-USERS>1</MAX-USERS>
<ALLOWED-USERS>1</ALLOWED-USERS>
<PERSON FNAME="CART" LNAME="DUDE" RESPONSIBLITY="CART DUDE" />
</ORDER-NUMBER>
</ORDER>
</SAPPHIRE>
I have the DTO as:
public class Sapphire : IXmlSerializable
{
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(XmlWriter writer)
{
throw new NotImplementedException();
}
}
回答1:
Implementing IXmlSerializable
is fairly tricky and potentially error prone (for example you need to make sure that your code correctly handles things like comments). For this example you should just be able to use XSD.exe to generate an appropriate class that uses attributes to control the xml serialisation.
You should use XSD.exe to generate a schema for your example xml fragment (which you ay need to tweak - if you hav an existing XSD schema then skip this step), then use it again to generate a class to use for serialisation.
If you really want to implement IXmlSerializable then try the following resources:
- How to Implement IXmlSerializable Correctly (CodeProject)
- Proper way to implement IXmlSerializable? (StackOverflow)
来源:https://stackoverflow.com/questions/5590592/help-with-implementing-ixmlserializable-on-this-xml