我是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>
老实说,我反序列化XML时去掉名称空间来避免这个问题。 –
我原来是这么做的(剥离出XmlRootAttribute中的Namespace =“”),但这并没有帮助。我不确定为什么xsd.exe会首先尝试将其包含在内。但很明显,错误< xmlns=''>指的是命名空间问题。我同意。但是什么名字空间? XML数据文件没有任何内容。 – superticker
Dr Dobbs Journal的文章http://www.drdobbs.com/windows/parsing-xml-files-in-net-using-c/184416669讨论了5种方法(包括使用LINQ),但我是试图采用“方法4”,这里使用XMLdeserialization。是的,我知道还有其他6种有效的方法。 – superticker