2016-12-26 56 views
0

我是XMLdeserialization的新手。我使用xsd.exe生成了對現有XML文件執行反序列化的對象類。當我運行,下面我的解決方案,我得到的錯誤在C#反序列化期間,獲取System.InvalidOperationException:< xmlns=''>不是預期的

System.InvalidOperationException:<的xmlns = ''>預計不會

的「S =(NewDataSet)xs.Deserialize(SR )「呼叫。我在Stackoverflow上看到了這個錯誤,每個人都說它在XMLRootAttribute行中。

[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 

我已經嘗試了各種修復此行,但沒有任何工作。有人可以糾正它或指向我解釋如何糾正它的一些文檔?非常感謝先進的。

此外,爲什麼xsd.exe首先不會生成正確的XmlRootAttribute行?我是我援引這個實用程序錯誤?或者在某些情況下xsd.exe實用程序無法處理?

public class Program 
{ 
    static void Main(string[] args) 
    { 
     SortedSet<string> symbolsEstablished = new SortedSet<string>(); 

     GetXmlDataSet("Expt 2buy.xml", ref symbolsEstablished); 
    } 

    public static void GetXmlDataSet(string fileName, ref SortedSet<string> symbols) 
    { 
     XmlSerializer xs = new XmlSerializer(typeof(NewDataSet)); 
     StreamReader sr = new StreamReader(@"C:\Users\mehl\AppData\Roaming\Fidelity Investments\WealthLabPro\1.0.0.0\Data\DataSets\" + fileName); 
     NewDataSet s = (NewDataSet)xs.Deserialize(sr); 
     Console.WriteLine(s.Items[0].DSString); 
     sr.Close(); 
    } 

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [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 DataSet 
    { 
     private string nameField; 
     private string scaleField; 
     private string barIntervalField; 
     private string dSStringField; 
     private string providerNameField; 

     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string Name 
     { 
      get { return this.nameField; } 
      set { this.nameField = value; } 
     } 

     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string Scale 
     { 
      get { return this.scaleField; } 
      set { this.scaleField = value; } 
     } 

     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string BarInterval 
     { 
      get { return this.barIntervalField; } 
      set { this.barIntervalField = value; } 
     } 

     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string DSString 
     { 
      get { return this.dSStringField; } 
      set { this.dSStringField = value; } 
     } 

     [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
     public string ProviderName 
     { 
      get { return this.providerNameField; } 
      set { this.providerNameField = value; } 
     } 
    } 

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [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 NewDataSet 
    { 
     private DataSet[] itemsField; 

     [System.Xml.Serialization.XmlElementAttribute("DataSet")] 
     public DataSet[] Items 
     { 
      get { return this.itemsField; } 
      set { this.itemsField = value; } 
     } 
    } 
} 

整個上面的代碼段被封裝在一個screener2wl命名空間中。

這裏是我在嘗試反序列化的XML文件:

<?xml version="1.0"?> 
<DataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Name>Expt 2buy</Name> 
    <Scale>Daily</Scale> 
    <BarInterval>0</BarInterval> 
    <DSString>AAL,AFSI,BEN,BIG,BLKB,CDK,COHR,CRUS,EGP,EPE,ETH,FB,HUM,LSTR,MDP,MSI,NYT,TAST,TER,TSO,TXN,UTHR,VASC,VLO,WRI,</DSString> 
    <ProviderName>FidelityStaticProvider</ProviderName> 
</DataSet> 
+0

老實說,我反序列化XML時去掉名稱空間來避免這個問題。 –

+0

我原來是這麼做的(剝離出XmlRootAttribute中的Namespace =「」),但這並沒有幫助。我不確定爲什麼xsd.exe會首先嚐試將其包含在內。但很明顯,錯誤< xmlns=''>指的是命名空間問題。我同意。但是什麼名字空間? XML數據文件沒有任何內容。 – superticker

+0

Dr Dobbs Journal的文章http://www.drdobbs.com/windows/parsing-xml-files-in-net-using-c/184416669討論了5種方法(包括使用LINQ),但我是試圖採用「方法4」,這裏使用XMLdeserialization。是的,我知道還有其他6種有效的方法。 – superticker

回答

0

使用XML LINQ:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 

      DataSet ds = doc.Descendants("DataSet").Select(x => new DataSet() { 
       nameField = (string)x.Element("Name"), 
       scaleField = (string)x.Element("Scale"), 
       barIntervalField = (string)x.Element("BarInterval"), 
       dSStringField = (string)x.Element("DSString"), 
       providerNameField = (string)x.Element("ProviderName") 
      }).FirstOrDefault(); 
     } 
    } 
    public partial class DataSet 
    { 
     public string nameField { get; set; } 
     public string scaleField { get; set; } 
     public string barIntervalField { get; set; } 
     public string dSStringField { get; set; } 
     public string providerNameField { get; set; } 
    } 
} 
0

我開始思考......是不是很奇怪會XSD.EXE提供一個代碼生成的解決方案,該解決方案定義了兩個獨立的 XmlRootAttribute節點的兩個? XML文件甚至可以有兩個根節點嗎?也許xsd.exe生成的解決方案不應該從字面上理解。 :-)

因此,編輯其解決方案,並刪除一個部分與第二個XmlRootAttribute定義後,我得到了下面的解決方案,它的工作原理。

namespace screener2wl 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      SortedSet<string> symbolsEstablished = new SortedSet<string>(); 
      GetXmlDataSet("Expt 2buy.xml", ref symbolsEstablished); 
     } 

     public static void GetXmlDataSet(string fileName, ref SortedSet<string> symbols) 
     { 
      XmlSerializer xs = new XmlSerializer(typeof(DataSet)); 
      StreamReader sr = new StreamReader(@"C:\Users\mehl\AppData\Roaming\Fidelity Investments\WealthLabPro\1.0.0.0\Data\DataSets\" + fileName); 
      DataSet s = (DataSet)xs.Deserialize(sr); 
      Console.WriteLine(s.DSString); 
      sr.Close(); 
     } 

     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
     [System.Xml.Serialization.XmlRootAttribute(IsNullable = false)] 
     public class DataSet 
     { 
      private string nameField; 
      private string scaleField; 
      private string barIntervalField; 
      private string dSStringField; 
      private string providerNameField; 

      [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
      public string Name 
      { 
       get { return this.nameField; } 
       set { this.nameField = value; } 
      } 

      [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
      public string Scale 
      { 
       get { return this.scaleField; } 
       set { this.scaleField = value; } 
      } 

      [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
      public string BarInterval 
      { 
       get { return this.barIntervalField; } 
       set { this.barIntervalField = value; } 
      } 

      [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
      public string DSString 
      { 
       get { return this.dSStringField; } 
       set { this.dSStringField = value; } 
      } 

      [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
      public string ProviderName 
      { 
       get { return this.providerNameField; } 
       set { this.providerNameField = value; } 
      } 
     } 
    } 
} 

底線,使用xsd.exe從您的XML數據文件生成的代碼解決方案作爲指導,而不是一個事實。這是一個偉大的工具,但需要與一粒鹽一起使用。 ...歡迎您對我的XML解決方案問題提出意見。

相關問題