将XML反序列化为从名称空间为

时间:2016-04-15 15:50:57

标签: c# xml serialization xsd

我将从这开始,我已经阅读了这个问题的所有其他答案,所有这些(虽然好的解决方案)在我的案例中不起作用

我使用

从我的xsd文件创建了一个c#类

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>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。没有。 我已经尝试删除命名空间并重新执行该类而不执行任何操作。

破坏我的大脑,因为一切看起来都是合法的。

1 个答案:

答案 0 :(得分: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上面的代码工作