这是我开始的方式:
System.Xml.Serialization.XmlRootAttribute xRoot = new System.Xml.Serialization.XmlRootAttribute();
xRoot.IsNullable = true;
xRoot.Namespace = "urn:schemas-microsoft-com:rowset";
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ProductSalesList), xRoot);
System.Xml.XmlReader reader = new System.Xml.XmlTextReader(path + "\\" + file);
eltOnly e = (eltOnly)serializer.Deserialize(reader);
但我不知道如何继续。这是xml文件:
<xml xmlns:s='uuid:00000000-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-0000-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly'>
<s:AttributeType name='Art' rs:number='1' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='string' dt:maxLength='255'/>
</s:AttributeType>
<s:AttributeType name='Name' rs:number='2' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='string' dt:maxLength='255'/>
</s:AttributeType>
<s:AttributeType name='Sum' rs:number='3' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='float' dt:maxLength='8' rs:precision='15' rs:fixedlength='true'/>
</s:AttributeType>
<s:AttributeType name='Cost' rs:number='4' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='float' dt:maxLength='8' rs:precision='15' rs:fixedlength='true'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row Art='0000000001' Name='Brand pils 0,2' Sum='153' Cost='304'/>
<z:row Art='0000000002' Name='Brand pils 0,25' Sum='11' Cost='25.300000000000004'/>
<z:row Art='0000000003' Name='Brand pils 0,5' Sum='3' Cost='13.799999999999999'/>
</rs:data>
</xml>
如何继续?我应该如何调用具有反序列化属性的类?
这就是我的所作所为:
[Serializable()]
public class Elt
{
[System.Xml.Serialization.XmlElement]
public string Art { get; set; }
[System.Xml.Serialization.XmlElement]
public string Name { get; set; }
[System.Xml.Serialization.XmlElement]
public float? Sum { get; set; }
[System.Xml.Serialization.XmlElement]
public float? Cost { get; set; }
}
[Serializable, System.Xml.Serialization.XmlRoot("eltOnly")]
public class eltOnly
{
[System.Xml.Serialization.XmlElement]
public List<Elt> Elt { get; set; }
}
我收到错误:XML文档中存在错误(1,2)。 “没想到。”
答案 0 :(得分:2)
这个linq2xml
应该做你想要的......
XDocument doc=XDocument.Load(yourXml);
XNamespace rs="urn:schemas-microsoft-com:rowset";
XNamespace z="#RowsetSchema";
var lstRows=doc.Descendants(rs+"data").Elements(z+"row").Select(x=>
new
{
art=x.Attribute("Art").Value,
name=x.Attribute("Name").Value,
sum=(float?)x.Attribute("Sum"),
cost=(float?)x.Attribute("Cost")
}
);
您现在可以遍历lstRows
foreach(var row in lstRows)
{
row.art;//string
row.name;//string
row.sum;//float?
row.cost;//float?
}