将 XML 反序列化为 c# class 派生自具有命名空间的 xsd.exe 时出错
Error when Deserializing XML into c# class derived from xsd.exe with namespace
我将首先阅读这个问题的所有其他答案,但所有这些(尽管是好的解决方案)在我的案例中都不起作用
我使用
从我的 xsd 文件创建了一个 c# class
xsd.exe /c neworder.xsd
它生成了一个 class 的 7000 多行,所以我将 post 它的相关部分。
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Generic;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.foo.com/schemas/w/v1.0")]
[System.Xml.Serialization.XmlRootAttribute("new-order", Namespace = "http://www.foo.com/schemas/w/v1.0")]
public partial class neworder
{
private List<customertype> customersField;
private string suppliercodeField;
private string versionField;
private static System.Xml.Serialization.XmlSerializer serializer;
public neworder()
{
this.customersField = new List<customertype>();
}
[System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
[System.Xml.Serialization.XmlArrayItemAttribute("customer",IsNullable = false)]
public List<customertype> customers
{
get
{
return this.customersField;
}
set
{
this.customersField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("supplier-code")]
public string suppliercode
{
get
{
return this.suppliercodeField;
}
set
{
this.suppliercodeField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("version")]
public string version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(neworder));
}
return serializer;
}
}
public static neworder Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((neworder)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
这只是一小段,其余的现在并不重要,因为我觉得如果解决了这个问题,我可以解决其余的问题。
这是 XML 文件的开头部分
<new-order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.foo.com/schema/w/v1.0" version="1.0" supplier-code="FAKECODE" schemaLocation="http://www.foo.com/schemas/w/v1.0/TransmissionEnvelope.xsd">
<customers>
<customer w-number="123456" customer-number="12345-12345">
<client-info>
<client-name>John Doe</client-name>
<w-id>433348</w-id>
</client-info>
<transferee-name>
<first>John</first>
<last>Doe</last>
</transferee-name>
<spouse-name>
<first />
<last />
</spouse-name>
<o-address>
<street1>123 Fake st</street1>
<city>Fakeville</city>
<state>CA</state>
<postal-code>90210</postal-code>
<country>USA</country>
</o-address>
<d-address>
<street1 />
<city>harbour</city>
<state>VA</state>
<postal-code>55555</postal-code>
<country>USA</country>
</d-address>
<contact-info>
<phone>
<phone-type>CELL</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>HOME</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>WORK</phone-type>
<phone-number />
</phone>
<email>johndoe@email.com</email>
<comments>Just any comments here</comments>
</contact-info>
</customer>
</customers>
</new-order>
我尝试用下面的方法反序列化它
XmlSerializer ser = new XmlSerializer(typeof(neworder));
neworder feed = (neworder)ser.Deserialize(new FileStream(filePath, FileMode.Open)) ;
我得到的错误是臭名昭著的:
XML 文档 (1, 2) 中存在错误。
http://www.foo.com/schema/w/v1.0'> 不是预期的。
我一遍又一遍地阅读关于确保根注释作为属性 XMLROOT 的内容,上面的内容就是这样。而且它有正确的命名空间。
我试过将 XmlRootAttribute 更改为 XmlRoot。没有什么。
我试过删除命名空间并重新执行 class 但什么也没有。
让我绞尽脑汁思考这里可能出了什么问题,因为一切似乎都是合法的。
试试这个....(对于您发布的 XML)
正在使用......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
类.......
[XmlRoot(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Clientinfo
{
[XmlElement(ElementName = "client-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Clientname { get; set; }
[XmlElement(ElementName = "w-id", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Wid { get; set; }
}
[XmlRoot(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Transfereename
{
[XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string First { get; set; }
[XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Last { get; set; }
}
[XmlRoot(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Spousename
{
[XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string First { get; set; }
[XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Last { get; set; }
}
[XmlRoot(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Oaddress
{
[XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Street1 { get; set; }
[XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string City { get; set; }
[XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string State { get; set; }
[XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Postalcode { get; set; }
[XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Country { get; set; }
}
[XmlRoot(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Daddress
{
[XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Street1 { get; set; }
[XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string City { get; set; }
[XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string State { get; set; }
[XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Postalcode { get; set; }
[XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Country { get; set; }
}
[XmlRoot(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Phone
{
[XmlElement(ElementName = "phone-type", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Phonetype { get; set; }
[XmlElement(ElementName = "phone-number", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Phonenumber { get; set; }
}
[XmlRoot(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Contactinfo
{
[XmlElement(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public List<Phone> Phone { get; set; }
[XmlElement(ElementName = "email", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Email { get; set; }
[XmlElement(ElementName = "comments", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Comments { get; set; }
}
[XmlRoot(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customer
{
[XmlElement(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Clientinfo Clientinfo { get; set; }
[XmlElement(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Transfereename Transfereename { get; set; }
[XmlElement(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Spousename Spousename { get; set; }
[XmlElement(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Oaddress Oaddress { get; set; }
[XmlElement(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Daddress Daddress { get; set; }
[XmlElement(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Contactinfo Contactinfo { get; set; }
[XmlAttribute(AttributeName = "w-number")]
public string Wnumber { get; set; }
[XmlAttribute(AttributeName = "customer-number")]
public string Customernumber { get; set; }
}
[XmlRoot(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customers
{
[XmlElement(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Customer Customer { get; set; }
}
[XmlRoot(ElementName = "new-order", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Neworder
{
[XmlElement(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Customers Customers { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "supplier-code")]
public string Suppliercode { get; set; }
[XmlAttribute(AttributeName = "schemaLocation")]
public string SchemaLocation { get; set; }
}
代码......
string strXML = File.ReadAllText("xml.xml");
byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
MemoryStream ms1 = new MemoryStream(bufXML);
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(Neworder));
try
{
using (XmlReader reader = new XmlTextReader(ms1))
{
Neworder deserializedXML = (Neworder)serializer.Deserialize(reader);
}// put a break point here and mouse-over deserializedXML….
}
catch (Exception ex)
{
throw;
}
我正在将您的 XML 读入应用程序生成文件夹中名为 xml.xml 的文件中的字符串...您需要从其他地方获取 XML 字符串或者创建 xml.xml 文件并保存你的 XML 以使上面的代码工作
我将首先阅读这个问题的所有其他答案,但所有这些(尽管是好的解决方案)在我的案例中都不起作用
我使用
从我的 xsd 文件创建了一个 c# classxsd.exe /c neworder.xsd
它生成了一个 class 的 7000 多行,所以我将 post 它的相关部分。
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Generic;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.foo.com/schemas/w/v1.0")]
[System.Xml.Serialization.XmlRootAttribute("new-order", Namespace = "http://www.foo.com/schemas/w/v1.0")]
public partial class neworder
{
private List<customertype> customersField;
private string suppliercodeField;
private string versionField;
private static System.Xml.Serialization.XmlSerializer serializer;
public neworder()
{
this.customersField = new List<customertype>();
}
[System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
[System.Xml.Serialization.XmlArrayItemAttribute("customer",IsNullable = false)]
public List<customertype> customers
{
get
{
return this.customersField;
}
set
{
this.customersField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("supplier-code")]
public string suppliercode
{
get
{
return this.suppliercodeField;
}
set
{
this.suppliercodeField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("version")]
public string version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(neworder));
}
return serializer;
}
}
public static neworder Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((neworder)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
这只是一小段,其余的现在并不重要,因为我觉得如果解决了这个问题,我可以解决其余的问题。
这是 XML 文件的开头部分
<new-order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.foo.com/schema/w/v1.0" version="1.0" supplier-code="FAKECODE" schemaLocation="http://www.foo.com/schemas/w/v1.0/TransmissionEnvelope.xsd">
<customers>
<customer w-number="123456" customer-number="12345-12345">
<client-info>
<client-name>John Doe</client-name>
<w-id>433348</w-id>
</client-info>
<transferee-name>
<first>John</first>
<last>Doe</last>
</transferee-name>
<spouse-name>
<first />
<last />
</spouse-name>
<o-address>
<street1>123 Fake st</street1>
<city>Fakeville</city>
<state>CA</state>
<postal-code>90210</postal-code>
<country>USA</country>
</o-address>
<d-address>
<street1 />
<city>harbour</city>
<state>VA</state>
<postal-code>55555</postal-code>
<country>USA</country>
</d-address>
<contact-info>
<phone>
<phone-type>CELL</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>HOME</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>WORK</phone-type>
<phone-number />
</phone>
<email>johndoe@email.com</email>
<comments>Just any comments here</comments>
</contact-info>
</customer>
</customers>
</new-order>
我尝试用下面的方法反序列化它
XmlSerializer ser = new XmlSerializer(typeof(neworder));
neworder feed = (neworder)ser.Deserialize(new FileStream(filePath, FileMode.Open)) ;
我得到的错误是臭名昭著的:
XML 文档 (1, 2) 中存在错误。 http://www.foo.com/schema/w/v1.0'> 不是预期的。
我一遍又一遍地阅读关于确保根注释作为属性 XMLROOT 的内容,上面的内容就是这样。而且它有正确的命名空间。
我试过将 XmlRootAttribute 更改为 XmlRoot。没有什么。 我试过删除命名空间并重新执行 class 但什么也没有。
让我绞尽脑汁思考这里可能出了什么问题,因为一切似乎都是合法的。
试试这个....(对于您发布的 XML)
正在使用......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
类.......
[XmlRoot(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Clientinfo
{
[XmlElement(ElementName = "client-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Clientname { get; set; }
[XmlElement(ElementName = "w-id", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Wid { get; set; }
}
[XmlRoot(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Transfereename
{
[XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string First { get; set; }
[XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Last { get; set; }
}
[XmlRoot(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Spousename
{
[XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string First { get; set; }
[XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Last { get; set; }
}
[XmlRoot(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Oaddress
{
[XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Street1 { get; set; }
[XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string City { get; set; }
[XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string State { get; set; }
[XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Postalcode { get; set; }
[XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Country { get; set; }
}
[XmlRoot(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Daddress
{
[XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Street1 { get; set; }
[XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string City { get; set; }
[XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string State { get; set; }
[XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Postalcode { get; set; }
[XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Country { get; set; }
}
[XmlRoot(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Phone
{
[XmlElement(ElementName = "phone-type", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Phonetype { get; set; }
[XmlElement(ElementName = "phone-number", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Phonenumber { get; set; }
}
[XmlRoot(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Contactinfo
{
[XmlElement(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public List<Phone> Phone { get; set; }
[XmlElement(ElementName = "email", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Email { get; set; }
[XmlElement(ElementName = "comments", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Comments { get; set; }
}
[XmlRoot(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customer
{
[XmlElement(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Clientinfo Clientinfo { get; set; }
[XmlElement(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Transfereename Transfereename { get; set; }
[XmlElement(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Spousename Spousename { get; set; }
[XmlElement(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Oaddress Oaddress { get; set; }
[XmlElement(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Daddress Daddress { get; set; }
[XmlElement(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Contactinfo Contactinfo { get; set; }
[XmlAttribute(AttributeName = "w-number")]
public string Wnumber { get; set; }
[XmlAttribute(AttributeName = "customer-number")]
public string Customernumber { get; set; }
}
[XmlRoot(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customers
{
[XmlElement(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Customer Customer { get; set; }
}
[XmlRoot(ElementName = "new-order", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Neworder
{
[XmlElement(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Customers Customers { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "supplier-code")]
public string Suppliercode { get; set; }
[XmlAttribute(AttributeName = "schemaLocation")]
public string SchemaLocation { get; set; }
}
代码......
string strXML = File.ReadAllText("xml.xml");
byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
MemoryStream ms1 = new MemoryStream(bufXML);
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(Neworder));
try
{
using (XmlReader reader = new XmlTextReader(ms1))
{
Neworder deserializedXML = (Neworder)serializer.Deserialize(reader);
}// put a break point here and mouse-over deserializedXML….
}
catch (Exception ex)
{
throw;
}
我正在将您的 XML 读入应用程序生成文件夹中名为 xml.xml 的文件中的字符串...您需要从其他地方获取 XML 字符串或者创建 xml.xml 文件并保存你的 XML 以使上面的代码工作