2016-04-15 48 views
0

我将开始阅读所有这个问题的其他答案,并且所有这些答案(尽管是好的解决方案)在我的情况下都不起作用将XML反序列化为c#类时从xsd.exe派生的命名空间导致的错误

我创建一个C#类从我的XSD文件与

XSD.EXE/C neworder.xsd

它产生的一类7000+线,所以我会后它的相关部分。

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>[email protected]</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。没有。 我试过删除名称空间并重新执行该类,但没有任何内容。

破坏我的大脑,因为一切似乎都合法。

+0

发布架构和XML文件。 – Aybe

+0

你可以发布一个完整的XML文件(也许是一个剥离)?我可以通过向'schema'添加's'来阻止异常:'xmlns =“http://www.foo.com/schemas/w/v1.0”',但我不知道如果通过仅使用“”元素的“xml文件”进行测试存在问题。 – Quantic

+0

添加了XML文件。不希望发布模式,因为它不是** my **模式。 我做了临时解决方法(我们将看看它是否适用于所有测试文档)是我使用xsd.exe从其中一个测试文档创建了一个XSD文件,然后我从中创建了该类。它在一个XML文档上工作,但我尝试了多个。 不是我想去的方式,但是,哦,好。 – reindeerKurt

回答

0

试试这个....(对于您发布的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保存为上述代码以工作

+0

这实际上很棒!很干净。非常感谢你。我仍然对这个类的xsd版本的实际错误感到好奇,因为**在理论上**,它应该可以工作,但我不知道为什么。这**虽然与XML文件一起工作。我需要测试多个样本。 – reindeerKurt

+0

很高兴帮助....也许选择这个解决方案作为答案,如果它已经解决了您的问题...干杯.... – Monty

+0

@reindeerKurt我不知道任何关于模式,但你的'schemaLocation'似乎格式不正确。首先它指向一个不在你的名字空间列表中的URI(你指向'http:// www.foo.com/schema/w/v1.0'',而你的名字空间有'http://www.foo.com/schemas/w/v1.0'。其次,它看起来应该是一个空格分隔的列表,你的'.xsd'在空格之后,参见[here](https://msdn.microsoft.com/ en-us/library/ms256100(v = vs.110).aspx),除非您想要:https://msdn.microsoft.com/en-us/library/ms256139(v=vs.110).aspx – Quantic

相关问题