帮助在此XML上实现IXmlSerializable [英] Help with implementing IXmlSerializable on this XML
问题描述
这里是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>
我的DTO为:
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();
}
}
推荐答案
实施 IXmlSerializable
相当棘手,并且容易出错(例如,您需要确保您的代码正确处理了注释之类的内容)。对于此示例,您应该只能够使用XSD.exe生成一个适当的类,该类使用属性来控制xml序列化。
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.
您应该使用 XSD.exe 可以为您的示例xml生成架构片段(您需要进行调整-如果您已有现有的XSD架构,则跳过此步骤),然后再次使用它来生成用于序列化的类。
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.
如果您确实要实现IXmlSerializable,请尝试以下资源:
If you really want to implement IXmlSerializable then try the following resources:
- How to Implement IXmlSerializable Correctly (CodeProject)
- Proper way to implement IXmlSerializable? (StackOverflow)
这篇关于帮助在此XML上实现IXmlSerializable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!