2016-12-26 154 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解决方案问题提出意见。

相关问题