使用 foreach 获取 XML 个节点的值
Get values of XML nodes with foreach
我正在使用 C# 和 XML-File 编写程序。我想读取 xml-nodes 的值,但我遇到了问题。在第二部分,我试图获取内容,它只执行一个循环而不是三个。第一个值是正确的。我不知道为什么它只进行第一个循环。我希望有人能帮助我。
我的XML-文件:
<?xml version="1.0" encoding="utf-8"?>
<lagerverwaltung>
<article>
<inventory id="1">
</inventory>
<orders>
<order id="1">
<id>1</id>
<idposition>1</idposition>
<content>
<idarticle amount="4">2</idarticle>
<idarticle amount="3">3</idarticle>
<idarticle amount="2">1</idarticle>
</content>
<idcustomer>2</idcustomer>
<orderdate>05.01.2018 15:10:44</orderdate>
<paydate>05.02.2018</paydate>
<mwst>7.7</mwst>
<total>1781.358</total>
</order>
</orders>
</article>
</lagerverwaltung>
我的 C# 代码:
List<Order> orderList = new List<Order>();
XmlDocument xml = new XmlDocument();
xml.Load(xmlFilePath);
XmlNodeList xnList = xml.SelectNodes("/lagerverwaltung/article/orders/order");
foreach (XmlNode xn in xnList)
{
// Is working
string id = xn["id"].InnerText;
string bestellPositionId = xn["idposition"].InnerText;
string kundeId = xn["idcustomer"].InnerText;
string bestelldatum = xn["orderdate"].InnerText;
string rechnungsDatum = xn["paydate"].InnerText;
string mwst = xn["mwst"].InnerText;
string rechnungsTotal = xn["total"].InnerText;
XmlNodeList xnInhalt = xml.SelectNodes("/lagerverwaltung/article/orders/order[@id='" + id + "']/content");
Dictionary<string, string> content= new Dictionary<string, string>();
foreach (XmlNode xmlNode in xnInhalt) // Does only one loop
{
string articleid = xmlNode["idarticle"].InnerText;
string amount = xmlNode["idarticle"].GetAttribute("amount");
content.Add(articleid, amount);
}
}
有单个content
节点,使用content/idarticle
获取内部集合:
XmlNodeList xnInhalt = xml.SelectNodes("/lagerverwaltung/article/orders/order[@id='" + id
+ "']/content/idarticle");
然后您将修改以下代码,因为 xmlNode 现在引用 idarticle
。例如,
string articleid = xmlNode.InnerText;
我正在使用 C# 和 XML-File 编写程序。我想读取 xml-nodes 的值,但我遇到了问题。在第二部分,我试图获取内容,它只执行一个循环而不是三个。第一个值是正确的。我不知道为什么它只进行第一个循环。我希望有人能帮助我。
我的XML-文件:
<?xml version="1.0" encoding="utf-8"?>
<lagerverwaltung>
<article>
<inventory id="1">
</inventory>
<orders>
<order id="1">
<id>1</id>
<idposition>1</idposition>
<content>
<idarticle amount="4">2</idarticle>
<idarticle amount="3">3</idarticle>
<idarticle amount="2">1</idarticle>
</content>
<idcustomer>2</idcustomer>
<orderdate>05.01.2018 15:10:44</orderdate>
<paydate>05.02.2018</paydate>
<mwst>7.7</mwst>
<total>1781.358</total>
</order>
</orders>
</article>
</lagerverwaltung>
我的 C# 代码:
List<Order> orderList = new List<Order>();
XmlDocument xml = new XmlDocument();
xml.Load(xmlFilePath);
XmlNodeList xnList = xml.SelectNodes("/lagerverwaltung/article/orders/order");
foreach (XmlNode xn in xnList)
{
// Is working
string id = xn["id"].InnerText;
string bestellPositionId = xn["idposition"].InnerText;
string kundeId = xn["idcustomer"].InnerText;
string bestelldatum = xn["orderdate"].InnerText;
string rechnungsDatum = xn["paydate"].InnerText;
string mwst = xn["mwst"].InnerText;
string rechnungsTotal = xn["total"].InnerText;
XmlNodeList xnInhalt = xml.SelectNodes("/lagerverwaltung/article/orders/order[@id='" + id + "']/content");
Dictionary<string, string> content= new Dictionary<string, string>();
foreach (XmlNode xmlNode in xnInhalt) // Does only one loop
{
string articleid = xmlNode["idarticle"].InnerText;
string amount = xmlNode["idarticle"].GetAttribute("amount");
content.Add(articleid, amount);
}
}
有单个content
节点,使用content/idarticle
获取内部集合:
XmlNodeList xnInhalt = xml.SelectNodes("/lagerverwaltung/article/orders/order[@id='" + id
+ "']/content/idarticle");
然后您将修改以下代码,因为 xmlNode 现在引用 idarticle
。例如,
string articleid = xmlNode.InnerText;