我有xml,它是由第三方软件组成的。我想将XML的输出部分中的子元素提取到c#类中。我该怎么做?
我的班级定义
class InstrumentParameters
{
public string id;
public string ChannelNumber;
public string InstrumentParameterType;
public string IsMetaData;
public string Name;
}
我的列表存储参数
List<InstrumentParameters> iParams = new List<InstrumentParameters>();
最后是XML
<SimulatorConfigurationFile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/Simulator.SharedInterfaces" z:Id="1" z:Type="SimulatorConfigurationFile" z:Assembly="Simulator.SharedInterfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null">
<Inputs xmlns="http://schemas.datacontract.org/2004/07/ConfigurationInterfaces" z:Id="2" z:Type="System.Collections.Generic.List`1[[ConfigurationInterfaces.InstrumentParameter, ConfigurationInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" z:Assembly="0">
<_items z:Id="3" z:Size="3">
<InstrumentParameter z:Id="4">
<ChannelNumber>0</ChannelNumber>
<InstrumentParameterType>Double</InstrumentParameterType>
<IsMetaData>false</IsMetaData>
<Name z:Id="5">Test Input Parameter 1</Name>
</InstrumentParameter>
<InstrumentParameter z:Id="6">
<ChannelNumber>0</ChannelNumber>
<InstrumentParameterType>Double</InstrumentParameterType>
<IsMetaData>false</IsMetaData>
<Name z:Id="7">Test Input Parameter 2</Name>
</InstrumentParameter>
<InstrumentParameter z:Id="8">
<ChannelNumber>0</ChannelNumber>
<InstrumentParameterType>Double</InstrumentParameterType>
<IsMetaData>false</IsMetaData>
<Name z:Id="9">Test Input Parameter 3</Name>
</InstrumentParameter>
</_items>
<_size>3</_size>
<_version>0</_version>
</Inputs>
<Outputs xmlns="http://schemas.datacontract.org/ConfigurationInterfaces" z:Id="10" z:Type="System.Collections.Generic.List`1[[ConfigurationInterfaces.InstrumentParameter, ConfigurationInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" z:Assembly="0">
<_items z:Id="11" z:Size="13">
<InstrumentParameter z:Id="12">
<ChannelNumber>0</ChannelNumber>
<InstrumentParameterType>Double</InstrumentParameterType>
<IsMetaData>false</IsMetaData>
<Name z:Id="13">0</Name>
</InstrumentParameter>
<InstrumentParameter z:Id="14">
<ChannelNumber>0</ChannelNumber>
<InstrumentParameterType>Double</InstrumentParameterType>
<IsMetaData>false</IsMetaData>
<Name z:Id="15">1000</Name>
</InstrumentParameter>
</_items>
<_size>13</_size>
<_version>0</_version>
</Outputs>
</SimulatorConfigurationFile>
在反序列化列表的第一项后,iParams将
id = 12
ChannelNumber = 0
InstrumentParamterType = Double
IsMetaData = false
Name = 0
是的,我尝试了以下方法。加载xdoc时没有任何问题。
try
{
// Get the actual output data
XmlDocument xdoc = new XmlDocument();
XmlReader rdr = cmd.ExecuteXmlReader();
if (rdr.Read())
{
xdoc.Load(rdr);
}
// Deserialize to IConfigurationFile
//XDocument xd = new XDocument(config);
// Use Linq
XElement el = XElement.Parse(xdoc.OuterXml);
rdr.Close();
var decs = el.Elements().Where(e => e.Name.LocalName == "Outputs").Descendants();
//var items = el.Elements("Outputs").Elements("_items").Descendants().DescendantNodes();
List<InstrumentParameters> iParams = new List<InstrumentParameters>();
foreach (var dec in decs)
{
InstrumentParameters iParam = new InstrumentParameters();
IEnumerable<XNode> a = dec.Nodes();
IEnumerator<XNode> b = a.GetEnumerator();
while (b.MoveNext())
{
XNode c = b.Current;
//var n = c.Document.Descendants();
// c.
}
}
我无法获得准确的值。任何帮助!
答案 0 :(得分:2)
Linq to xml会更容易使用
XNamespace z = "http://schemas.microsoft.com/2003/10/Serialization/";
XNamespace ns = "http://schemas.datacontract.org/ConfigurationInterfaces";
var list = XDocument.Load(filename)
.Descendants(ns + "Outputs")
.First()
.Descendants(ns + "InstrumentParameter")
.Select(e => new
{
id = (string)e.Attribute(z + "Id"),
ChannelNumber = (string)e.Element(ns + "ChannelNumber"),
InstrumentParameterType = (string)e.Element(ns + "InstrumentParameterType"),
IsMetaData = (string)e.Element(ns + "IsMetaData"),
Name = (string)e.Element(ns + "Name"),
})
.ToList();