【发布时间】:2018-02-06 21:30:09
【问题描述】:
我有一个大型 XML 文档,已加载到 XmlDocument 中,我想使用 XmlSerializer 类将其中的选定元素反序列化为使用 xsd.exe 生成的 .NET 类。
这是迄今为止我尝试过的 MCVE; xsd 和生成的类在帖子的末尾。正如代码中的 cmets 所述,我得到了一个 InvalidOperationException - <Cars xmlns:'http://MyNamespace' /> was not expected:
static string XmlContent = @"
<RootNode xmlns=""http://MyNamespace"">
<Cars>
<Car make=""Volkswagen"" />
<Car make=""Ford"" />
<Car make=""Opel"" />
</Cars>
</RootNode>";
static void TestMcve()
{
var doc = new XmlDocument();
doc.LoadXml(XmlContent);
var nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("myns", "http://MyNamespace");
var rootSerializer = new XmlSerializer(typeof(RootNode));
var root = (RootNode) rootSerializer.Deserialize(new XmlNodeReader(doc));
Console.WriteLine(root.Cars[0].make); // Works fine so far
var node = doc.DocumentElement.SelectSingleNode("myns:Cars", nsMgr);
Console.WriteLine(node.OuterXml);
var carSerializer = new XmlSerializer(typeof(Car));
using (var reader = new XmlNodeReader(node))
{
// What I want is a list of Car instances deserialized from
// the Car child elements of the Cars element.
// The following line throws an InvalidOperationException
// "<Cars xmlns:'http://MyNamespace' /> was not expected"
// If I change SelectSingleNode above to select "myns:Cars/myns:Car"
// I get "<Car xmlns:'http://MyNamespace' /> was not expected"
var result = carSerializer.Deserialize(reader);
}
}
我还想随后更新我的Car 类实例,并使用XmlSerializer 将其插入回文档中,这是后续问题How to insert a node in a large document using XmlSerializer
的主题。
xsd 和生成的类如下:
<xs:schema xmlns="http://MyNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MyNamespace"
elementFormDefault="qualified" attributeFormDefault="unqualified"
version="3.9.0.8">
<xs:complexType name="Cars">
<xs:sequence>
<xs:element name="Car" type="Car" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Car">
<xs:attribute name="make" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="RootNode">
<xs:sequence>
<xs:element name="Cars" type="Cars" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:element name="RootNode" type="RootNode" />
</xs:schema>
xsd.exe 生成的代码:
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://MyNamespace")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://MyNamespace", IsNullable=false)]
public partial class RootNode {
private Car[] carsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
public Car[] Cars {
get {
return this.carsField;
}
set {
this.carsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://MyNamespace")]
public partial class Car {
private string makeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string make {
get {
return this.makeField;
}
set {
this.makeField = value;
}
}
}
【问题讨论】:
标签: c# .net xml xsd xmlserializer