我尝试从XML文件中读取。使用c#XmlSerializer创建XML文件。
xml文件看起来像
<command>
<Name>one</Name>
<data>
<int>46</int>
<int>49</int>
<int>50</int>
</data>
<rangeData>
<int>36</int>
<int>29</int>
<int>10</int>
</rangeData>
</command>
读取xml文件我使用以下代码
string filename = "01.xml";
XmlDocument xdc = new xmlDocument();
xdc.Load(filename);
XmlNodeList xnlNodes = xelRoot.SelectNodes("command");
foreach(XMLNode nxmNode in xnlNodes)
{
XmlElement element = (XmlElement)xndNode;
string Name = Convert.ToString(xndNode{"Name"].InnerText);
data = new int[]
{
Convert.ToInt32(element.GetElementsByTagName("int")[0].InnerText),
Convert.ToInt32(element.GetElementsByTagName("int")[1].InnerText),
Convert.ToInt32(element.GetElementsByTagName("int")[2].InnerText),
};
rangeData = new int[]
{
Convert.ToInt32(element.GetElementsByTagName("int")[0].InnerText),
Convert.ToInt32(element.GetElementsByTagName("int")[1].InnerText),
Convert.ToInt32(element.GetElementsByTagName("int")[2].InnerText),
};
}
}
我的问题是,我不知道如何选择正确的子节点。我希望有人可以帮助我。感谢
答案 0 :(得分:1)
以下是您的方法:
string filename = "01.xml";
XmlDocument xdc = new XmlDocument();
xdc.Load(filename);
XmlNodeList xnlNodes = xdc.SelectNodes("command");
foreach (XmlNode xnlNode in xnlNodes)
{
XmlElement element = (XmlElement)xnlNode;
string Name = Convert.ToString(xnlNode["Name"].InnerText);
int[] data = new int[]
{
Convert.ToInt32(element.GetElementsByTagName("data")[0].ChildNodes[0].InnerText),
Convert.ToInt32(element.GetElementsByTagName("data")[0].ChildNodes[1].InnerText),
Convert.ToInt32(element.GetElementsByTagName("data")[0].ChildNodes[2].InnerText)
};
int[] rangeData = new int[]
{
Convert.ToInt32(element.GetElementsByTagName("rangeData")[0].ChildNodes[0].InnerText),
Convert.ToInt32(element.GetElementsByTagName("rangeData")[0].ChildNodes[1].InnerText),
Convert.ToInt32(element.GetElementsByTagName("rangeData")[0].ChildNodes[2].InnerText)
};
}
答案 1 :(得分:0)
我建议使用LINQ来读取XML:
var xdoc = XDocument.Load(filename);
var cmd = xdoc.Root;
var command = new {
Name = (string)cmd.Element("Name"),
Data = cmd.Element("data").Elements().Select(e => (int)e).ToList(),
RangeData = cmd.Element("rangeData").Elements().Select(e => (int)e).ToList()
};
输出:
{
Name: "one",
Data: [ 46, 49, 50 ],
RangeData: [ 36, 29, 10 ]
}
如果你没有提供整个xml,但只是它的一部分,实际上你有很多命令,那么解析就像
var commands =
from cmd in xdoc.Descendands("command")
select new {
Name = (string)cmd.Element("Name"),
Data = cmd.Element("data").Elements().Select(e => (int)e).ToList(),
RangeData = cmd.Element("rangeData").Elements().Select(e => (int)e).ToList()
}
答案 2 :(得分:0)
以下是如何使用xpath
访问节点的示例 XmlDocument xdc = new XmlDocument();
xdc.Load(filename);
var nodes = xdc.SelectNodes("command/data/int");
foreach (XmlNode node in nodes)
{
string s = node.InnerText;
}
要了解有关XPath的更多信息,请查看this tutorial
答案 3 :(得分:0)
你可以在这里使用XPath
void Main()
{
var doc = new XmlDocument();
doc.LoadXml(xml);
var name = doc.DocumentElement
.SelectSingleNode("/command/Name")
.InnerText;
var data = doc.DocumentElement
.SelectNodes("/command/data/int")
.OfType<XmlNode>()
.Select(x=> Int32.Parse(x.InnerText))
.ToArray();
var rangeData = doc.DocumentElement
.SelectNodes("/command/rangeData/int")
.OfType<XmlNode>()
.Select(x => Int32.Parse(x.InnerText))
.ToArray();
}
string xml = @"
<command>
<Name>one</Name>
<data>
<int>46</int>
<int>49</int>
<int>50</int>
</data>
<rangeData>
<int>36</int>
<int>29</int>
<int>10</int>
</rangeData>
</command>
";