【问题标题】:Trouble deserializing a document containing xsi:type attributes反序列化包含 xsi:type 属性的文档时遇到问题
【发布时间】:2013-01-20 15:57:16
【问题描述】:

解析 XML 文件时,serializer.Deserialize(reader) 出现错误: Namespace prefix \"java\" not defined

我的 XML http://www.virtualdtm.ru/files/serverReplays/rFactorHotlapsData.xml

我的代码

var serializer = new XmlSerializer(typeof(rootDB));
var reader = new XmlTextReader(Server.MapPath(Url.Content("~/Content/xml/rFactorHotlapsData.xml")));
var rFactorHotLapsData = (rootDB)serializer.Deserialize(reader);

我的班级

//------------------------------------------------------------------------------
// <auto-generated>
//     Этот код создан программой.
//     Исполняемая версия:4.0.30319.17929
//
//     Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае
//     повторной генерации кода.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;
namespace net.vmso.cpanel.Models.rFactorHotLaps
{
}

// 
// Этот исходный код был создан с помощью xsd, версия=4.0.30319.17929.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class rootDB {

    private rootDBListOfCategory[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("listOfCategory", Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
    public rootDBListOfCategory[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategory {

    private string categoryNameField;

    private rootDBListOfCategoryListOfTrack[] listOfTrackField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string categoryName {
        get {
            return this.categoryNameField;
        }
        set {
            this.categoryNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("listOfTrack", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public rootDBListOfCategoryListOfTrack[] listOfTrack {
        get {
            return this.listOfTrackField;
        }
        set {
            this.listOfTrackField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategoryListOfTrack {

    private string trackNameField;

    private rootDBListOfCategoryListOfTrackSlowestLapRecord[] slowestLapRecordField;

    private rootDBListOfCategoryListOfTrackListOfLapRecord[] listOfLapRecordField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string trackName {
        get {
            return this.trackNameField;
        }
        set {
            this.trackNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("slowestLapRecord", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public rootDBListOfCategoryListOfTrackSlowestLapRecord[] slowestLapRecord {
        get {
            return this.slowestLapRecordField;
        }
        set {
            this.slowestLapRecordField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("listOfLapRecord", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public rootDBListOfCategoryListOfTrackListOfLapRecord[] listOfLapRecord {
        get {
            return this.listOfLapRecordField;
        }
        set {
            this.listOfLapRecordField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDBListOfCategoryListOfTrackSlowestLapRecord {

    private string driverNameField;

    private string sessionNameField;

    private string dateField;

    private string vehicleModNameField;

    private string vehicleNameField;

    private string vehicleCategoryField;

    private string lapTimeField;

    private string lastLapTimeField;

    private string rankField;

    private string sessionIdField;

    private string lapCountField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string driverName {
        get {
            return this.driverNameField;
        }
        set {
            this.driverNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string sessionName {
        get {
            return this.sessionNameField;
        }
        set {
            this.sessionNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string date {
        get {
            return this.dateField;
        }
        set {
            this.dateField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string vehicleModName {
        get {
            return this.vehicleModNameField;
        }
        set {
            this.vehicleModNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string vehicleName {
        get {
            return this.vehicleNameField;
        }
        set {
            this.vehicleNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string vehicleCategory {
        get {
            return this.vehicleCategoryField;
        }
        set {
            this.vehicleCategoryField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string lapTime {
        get {
            return this.lapTimeField;
        }
        set {
            this.lapTimeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string lastLapTime {
        get {
            return this.lastLapTimeField;
        }
        set {
            this.lastLapTimeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string rank {
        get {
            return this.rankField;
        }
        set {
            this.rankField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string sessionId {
        get {
            return this.sessionIdField;
        }
        set {
            this.sessionIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string lapCount {
        get {
            return this.lapCountField;
        }
        set {
            this.lapCountField = value;
        }
    }
}

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class rootDBListOfCategoryListOfTrackListOfLapRecord {

        private string driverNameField;

        private string sessionNameField;

        private string dateField;

        private string vehicleModNameField;

        private string vehicleNameField;

        private string vehicleCategoryField;

        private string lapTimeField;

        private string lastLapTimeField;

        private string rankField;

        private string sessionIdField;

        private string lapCountField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string driverName {
            get {
                return this.driverNameField;
            }
            set {
                this.driverNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string sessionName {
            get {
                return this.sessionNameField;
            }
            set {
                this.sessionNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string date {
            get {
                return this.dateField;
            }
            set {
                this.dateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string vehicleModName {
            get {
                return this.vehicleModNameField;
            }
            set {
                this.vehicleModNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string vehicleName {
            get {
                return this.vehicleNameField;
            }
            set {
                this.vehicleNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string vehicleCategory {
            get {
                return this.vehicleCategoryField;
            }
            set {
                this.vehicleCategoryField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string lapTime {
            get {
                return this.lapTimeField;
            }
            set {
                this.lapTimeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string lastLapTime {
            get {
                return this.lastLapTimeField;
            }
            set {
                this.lastLapTimeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string rank {
            get {
                return this.rankField;
            }
            set {
                this.rankField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string sessionId {
            get {
                return this.sessionIdField;
            }
            set {
                this.sessionIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string lapCount {
            get {
                return this.lapCountField;
            }
            set {
                this.lapCountField = value;
            }
        }
    }

[1]:

【问题讨论】:

  • xsi:type 设置为 java:com.prorfactor.top100.database.Category。此类型的前缀 java 未链接到命名空间。
  • flup,感谢您的评论,我并不缺乏处理问题本身的知识。
  • 你能修改文档吗?
  • 此 XML 文档由第三方软件生成

标签: c#-4.0 xml-parsing xsd xsitype


【解决方案1】:

未定义 java 命名空间前缀,但在 .xml 文档的 xsi:type 属性中使用了它。如果您将定义它的属性添加到文档的根目录,错误就会消失。

<rootDB xmlns:java="http://tempuri3.org">

正如我们后来发现的那样,除此之外,xsi:type 正在搞乱类的生成。很糟糕。

使用 XmlSerializer

<listOfTrack xsi:type="java:com.prorfactor.top100.database.Track">

元素的名称是listOfTrack,在默认命名空间中。然而,它的类型在 java 前缀所指的命名空间中是 com.prorfactor.top100.database.Track。这是因为xsi:type 属性改变了它。 xsd.exe 可以解决这个问题,但需要一点帮助。

我让它工作的方式是创建两个.xsds。一个用于 java 命名空间,另一个用于文档的命名空间。他们相互导入。 作为一个例子,我将展示描述文档开头的部分,其中名称和类型跳来跳去很多。

<rootDB xmlns="http://tempuri2.org"
        xmlns:java="http://tempuri3.org">
  <listOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="java:com.prorfactor.top100.database.Category">
    <listOfTrack xsi:type="java:com.prorfactor.top100.database.Track">

对于我称为http://tempuri2.org 的默认命名空间,架构必须列出元素的名称,因为它们位于默认命名空间中:

  <xs:element name="rootDB" >
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="listOfCategory" />
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:element name="listOfCategory"
              type="java:com.prorfactor.top100.database.Category">
  </xs:element>

  <xs:element name="listOfTrack"
              type="java:com.prorfactor.top100.database.Track">
  </xs:element>

我称之为http://tempuri3.org 的 java 命名空间的架构列出了使用 xsi:type 分配给元素的复杂类型:

  <xs:complexType name="com.prorfactor.top100.database.Category"
        xmlns:orig="http://tempuri2.org">
    <xs:sequence>
      <xs:element ref="orig:listOfTrack" minOccurs="0" maxOccurs="unbounded" />
      <xs:element ref="orig:categoryName"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="com.prorfactor.top100.database.Track"
        xmlns:orig="http://tempuri2.org">
    <xs:sequence>
      <xs:element ref="orig:slowestLapRecord" />
      <xs:element ref="orig:listOfLapRecord" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element ref="orig:trackName"/>
    </xs:sequence>
  </xs:complexType>

我编译的这两个.xsds 和生成的类设法反序列化 .xml 文件。

如果我手动添加 java 命名空间,并为 .xml 文件指定默认命名空间

所以,老实说,我认为你最好使用 LINQ to XML:

使用 LINQ to XML

XDocument xElement = XDocument.Load(Server.MapPath(Url.Content("~/Content/xml/rFactorHotlapsData.xml")));

// just some selects to show how you select them
var cats = from cat in xElement.Descendants("listOfCategory")
           select new
           {
              Name = cat.Descendants("categoryName").FirstOrDefault().Value,
              Track = from track in cat.Descendants("listOfTrack")
                      select new 
                      {
                         Name = track.Descendants("trackName").FirstOrDefault().Value,
                         LapRecord = from record in cat.Descendants("listOfLapRecord")
                                     select new
                                     {
                                        driverName = record.Descendants("driverName").FirstOrDefault().Value,
                                        sessionName = record.Descendants("sessionName").FirstOrDefault().Value,
                                        date = record.Descendants("date").FirstOrDefault().Value
                                     }
                       }
           };

// And a demo of how to iterate over the result of the select
foreach (var category in cats)
{
   Console.WriteLine(category.Name);
   foreach (var track in category.Track)
   {
      Console.WriteLine("  " + track.Name);
      foreach (var lapRecord in track.LapRecord)
      {
         Console.WriteLine("      " + lapRecord.driverName);
         Console.WriteLine("      " + lapRecord.sessionName);
         Console.WriteLine("      " + lapRecord.date);
      }
   }
}

【讨论】:

  • 新错误:Указанный тип не был распознан: имя="com.prorfactor.top100.database.Category", пространство имен="java.oracle.com", 。 /跨度>
  • 感谢您的回复,我现在试试。
  • 您的反序列化器也不使用 java 命名空间。根元素也需要在命名空间中。你是如何生成反序列化器的?来自 XSD?
  • 您可以手动将相同(组成)的命名空间添加到根元素的注释中:[System.Xml.Serialization.XmlRootAttribute(Namespace="http://java.oracle.com", IsNullable=false)] 但也许最好也看看 .xsd
  • 是的,我从 XML 生成 XSD 并通过 XSD.exe 工具从 XSD 生成类文件
猜你喜欢
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多