从XML节点读取值?

时间:2014-05-22 08:05:08

标签: c# xml

我试图从XML字符串中提取信息,但我正在努力循环遍历节点。

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(OrderXml);
XmlNodeList parentNode = xmlDoc.GetElementsByTagName("SalesOrders");
XmlNodeList SalesOrderNode = xmlDoc.GetElementsByTagName("SalesOrder");
XmlNodeList parentNode2 = xmlDoc.GetElementsByTagName("WarningMessages");
foreach (XmlNode childrenNode in parentNode2)
{
  string test = childrenNode.Value.ToString();
}

我想获取SalesOrder编号,在这种情况下是它的r31283,之后我想循环遍历WarningMessages的内容。我需要获取每个警告说明的行号和股票代码。

我的XML:

<?xml version="1.0" encoding="Windows-1252"?>
<SalesOrders Language='05' Language2='EN' CssStyle='' DecFormat='1' DateFormat='01' Role='01' Version='6.1.085' OperatorPrimaryRole='100'>
<TransmissionHeader>
<TransmissionReference>00000000000003</TransmissionReference>
<SenderCode/> 
<DatePrepared>2014-05-22</DatePrepared>
<TimePrepared>09:12</TimePrepared>
</TransmissionHeader>
<Order>
<CustomerPoNumber>RQ140522-33</CustomerPoNumber>
<SalesOrder>r31283</SalesOrder>
<OrderActionType>A</OrderActionType>
<BackOrderComment>One or more lines have been placed on back order for order 'r31283'</BackOrderComment>
<WarningMessages>
<WarningDescription>Line 0001 for stock code 'NN0410BP01' was placed on back order</WarningDescription>
<WarningDescription>Line 0002 for stock code 'NN0400GR08' was placed on back order</WarningDescription>
<WarningDescription>Line 0003 for stock code 'NN0410BN01' was placed on back order</WarningDescription>
<WarningDescription>Line 0004 for stock code 'NN0370BH01' was placed on back order</WarningDescription>
<WarningDescription>Line 0005 for stock code 'NN0370BH02' was placed on back order</WarningDescription>
<WarningDescription>Line 0006 for stock code 'NN0390BC01' was placed on back order</WarningDescription>
<WarningDescription>Line 0007 for stock code 'NN0410HL01' was placed on back order</WarningDescription>
<WarningDescription>Line 0008 for stock code 'NN0410FD07' was placed on back order</WarningDescription>
<WarningDescription>Line 0009 for stock code 'NN0410FD08' was placed on back order</WarningDescription>
<WarningDescription>Line 0010 for stock code 'NN0400VL02' was placed on back order</WarningDescription>
</WarningMessages>
</Order>
</SalesOrders>

4 个答案:

答案 0 :(得分:4)

我建议使用Linq to Xml:

var xdoc = XDocument.Load(path_to_xml);
Regex regex = new Regex(@"Line (?<line>\d+) for stock code '(?<code>\w+)'");
var orders = from o in xdoc.Root.Elements("Order")
             select new
             {
                 SalesOrder = (string)o.Element("SalesOrder"),
                 WarningMessages =
                     from m in o.Element("WarningMessages").Elements()
                     let match = regex.Match((string)m)
                     where match.Success
                     select new
                     {
                         Line = match.Groups["line"].Value,
                         Code = match.Groups["code"].Value
                     }
             };

输出:

[
  {
     SalesOrder: "r31283",
     WarningMessages: [
       { Line: "0001", Code: "NN0410BP01" },
       { Line: "0002", Code: "NN0400GR08" },
       { Line: "0003", Code: "NN0410BN01" },
       { Line: "0004", Code: "NN0370BH01" },
       { Line: "0005", Code: "NN0370BH02" },
       { Line: "0006", Code: "NN0390BC01" },
       { Line: "0007", Code: "NN0410HL01" },
       { Line: "0008", Code: "NN0410FD07" },
       { Line: "0009", Code: "NN0410FD08" },
       { Line: "0010", Code: "NN0400VL02" }
    ]
  }
]

答案 1 :(得分:1)

您可以使用LINQ to XML

来完成
// Load XML file.
var doc = XDocument.Load(path);

// Find SalesOrder element.
var salesOrder = (from c in doc.Descendants("SalesOrder")
                 select c.Value).FirstOrDefault();

// Find warning message elements.
var warningMessages = (from c in doc.Descendants("WarningDescription")
                      where c.Parent != null && c.Parent.Name == "WarningMessages"
                      select c.Value).ToList();

然后循环思考警告信息

foreach(var warningMessage in warningMessages)
{
    // Handle warning message...
}

答案 2 :(得分:0)

首先使用XDocument类,以便可以使用LINQ to XML 然后查询这个docuemnt。使用Descendants方法获取doc中的所有元素。 然后使用Name属性搜索具有SalesOrder标记名称的那些。 因为你只希望一个匹配的元素在结果上使用First()方法来获得匹配的标记值。

    XDocument doc = XDocument.Load("some.xml");
    var result = from n in doc.Descendants() where n.Name == "SalesOrder" select n.Value;
    string val = result.First();

答案 3 :(得分:0)

试试这样:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(OrderXml);
XmlNode parentNode = xmlDoc.SelectSingleNode("SalesOrders");
XmlNode SalesOrderNode = xmlDoc.SelectSingleNode("SalesOrder");
XmlNodeList lstWarning = xmlDoc.SelectNodes("WarningMessages/WarningDescription");
foreach (XmlNode childrenNode in lstWarning)
{ 
    string test = childrenNode.InnerText;
}

还阅读有关XPath的内容。