0

XMLファイルを解析すると、次のエラーが発生しますserializer.Deserialize(reader)Namespace prefix \"java\" not defined

私のXMLhttp ://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]:

4

1 に答える 1

1

java名前空間プレフィックスは定義されていませんが、.xmlドキュメントのxsi:type属性で使用されます。それを定義する属性をドキュメントのルートに追加すると、エラーはなくなります。

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

後でわかったように、これに加えて、xsi:typeはクラスの生成を台無しにしています。ひどく。

XmlSerializerの使用

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

要素の名前はlistOfTrack、デフォルトの名前空間ではです。ただし、そのタイプはcom.prorfactor.top100.database.Track、Javaプレフィックスが参照している名前空間にあります。これは、xsi:type属性によって変更されるために発生します。xsd.exeはこれに対処できますが、少し助けが必要です。

私がそれを機能させる方法は、2つ.xsdのを作成することです。1つはJava名前空間用で、もう1つはドキュメントの名前空間用です。彼らはお互いをインポートします。例として、名前とタイプが頻繁に飛び交うドキュメントの開始を説明するビットを示します。

<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>

私が呼び出したjava名前空間のスキーマには、http://tempuri3.orgxsi: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>

私がコンパイルしたこれらの2つと.xsd、生成されたクラスは、.xmlファイルを逆シリアル化することに成功しました。

java名前空間を手動で追加し、さらにデフォルトの名前空間を.xmlファイルに指定する場合

したがって、正直なところ、LINQtoXMLを使用する方が良いと思います。

LINQtoXMLの使用

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);
      }
   }
}
于 2013-01-20T16:23:43.937 に答える