从使用XSD.exe生成的XML反序列化类

问题描述 投票:2回答:2

我有一个.xsd生成的类(使用xsd.exe)我可以序列化很好,但是当我尝试反序列化时,我得到错误:

{"<XMLLanguages xmlns='http://tempuri.org/XMLLanguages.xsd'> was not expected."}

我搜索了几个小时,发现大多数人的问题在于没有在xsd / xml中声明命名空间,没有在类中定义命名空间等,但我找不到解决问题的方法。

以下是相关课程的代码段。

    <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLLanguages"
    targetNamespace="http://tempuri.org/XMLLanguages.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLLanguages.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="XMLLanguages">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Tier" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="L" minOccurs="1" maxOccurs="unbounded" type="Language"/>
            </xs:sequence>
            <xs:attribute name="TierID" type="xs:int"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Language">
    <xs:sequence>
      <xs:element name="LangID" type="xs:int"/>
      <xs:element name="Tier" type="xs:int"/>
      <xs:element name ="Name" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name ="PassRate" type="xs:int"/>
  </xs:complexType>
</xs:schema>

和班级:

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/XMLLanguages.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLLanguages.xsd", IsNullable = false)]
public partial class XMLLanguages
{
    private List<XMLLanguagesTier> tierField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Tier")]
    public List<XMLLanguagesTier> Tiers {
        get {
            return this.tierField;
        }
        set {
            this.tierField = value;
        }
    }
}

并且XML中的行导致错误:

<XMLLanguages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/XMLLanguages.xsd">

反序列化方法:

public static object Deserialize(XmlDocument xml, Type type)
    {
        XmlSerializer s = new XmlSerializer(type);
        string xmlString = xml.OuterXml.ToString();
        byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlString);
        MemoryStream ms = new MemoryStream(buffer);
        XmlReader reader = new XmlTextReader(ms);
        Exception caught = null;

        try
        {
            object o = s.Deserialize(reader);
            return o;
        }

        catch (Exception e)
        {
            caught = e;
        }
        finally
        {
            reader.Close();

            if (caught != null)
                throw caught;
        }
        return null;
    }
c# xml xsd.exe
2个回答
0
投票

我使用以下代码,它工作正常。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Report"
           targetNamespace="http://www.xyz.com/Report.xsd"
           elementFormDefault="qualified"
           xmlns="http://www.xyz.com/Report.xsd"
           xmlns:mstns="http://www.xyz.com/Report.xsd"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="Report">
  <xs:complexType>
  ...

我使用xsd.exe和/ n:xyz命名空间选项。

这似乎工作正常,所以我认为你的问题必须与tempuri.org域名。

希望这个对你有帮助。

理查德。


0
投票

您需要从根目录中删除targetNamespace属性,并在XMLLanguages节点之前添加以下节点:

<xs:import namespace="http://www.w3.org/2001/XMLSchema"/>

上面的内容会让你反序列化,但我会感觉到其他问题。您遇到的问题是,当您定义多个复杂类型时,您无法使用targetNamespace属性 - 欢迎使用schema命名空间地狱......

© www.soinside.com 2019 - 2024. All rights reserved.