2016-04-15 8 views
0

私はこの問題の回答をすべて読みましたが、私の場合はすべて解決しましたが(良い解決策ですが)、xsd.exeから派生したxsd.exeからXMLをデシリアライズする際にエラーが発生しました。

私はそれの関連する部分を投稿しますので、それは7000+ラインのクラスを生成し

Xsd.exeでは/ C neworder.xsd

と私のXSDファイルからのAC#クラスを作成しました。ほんの少しの抜粋です

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(); 
       } 
      } 
     } 

私はこれを解決しますならば、私は残りの部分を解決することができるように感じるように、残りは今それは重要ではありません。

これは私が私が手にエラーが悪名高いです以下

XmlSerializer ser = new XmlSerializer(typeof(neworder)); 
    neworder feed = (neworder)ser.Deserialize(new FileStream(filePath, FileMode.Open)) ; 

でそれをデシリアライズしよう

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

XMLファイルの先頭部分である:

ありXML文書のエラーです(1,2)。 http://www.foo.com/schema/w/v1.0 '>は期待されていませんでした。

私は、上記のことを行う属性XMLROOTとしてルートノートを確認することについて何度も何度も読んできました。そしてそれには正しい名前空間があります。

XmlRootAttributeをXmlRootに変更しようとしました。何もない。 私は名前空間を削除して、クラスを再実行しようとしましたが、何もしませんでした。

すべてが正当だと思われるので、ここで間違っているかもしれないことに私の脳を壊す。

+0

スキーマとXMLファイル。 – Aybe

+0

完全なxmlファイルを投稿できますか(おそらく1つは削除されています)? 'xmlns =" http://www.foo.com/schemas/w/v1.0 "'のように 'schema'に' s'を追加して例外を止めることができましたが、私は知らない「xmlファイル」でテストすることによって問題が発生した場合は、「」要素のみ*です。 – Quantic

+0

XMLファイルが追加されました。 ** my **スキーマではないので、スキーマを投稿したくありません。 私が一時的な回避策としてやったのは(私たちはすべてのテストドキュメントを表示するかどうかを見ています)、私はそれからクラスを作成したテストドキュメントのxsd.exeを使ってXSDファイルを作成しました。これは、1つのXML文書で動作しますが、私は複数試みています。 私は行きたいとは思いませんが、まあまあです。 – reindeerKurt

答えて

0

....これを試してみてください

使用.....

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

コード(あなたが投稿したXML用) .....

 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'は不正な形式です。 1つは、あなたの名前空間リストにない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

関連する問題