简体   繁体   English

如何读取特定的XML值?

[英]How to read specific XML values?

This is given XML: 这是给定的XML:

<NewDataSet>
  <Table>
    <ProductCode>0120314</ProductCode>
    <SpecificationItemName>USB</SpecificationItemName>
    <SpecificationItemValues>&lt;Values xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;Value&gt;USB 2.0&lt;/Value&gt;&lt;/Values&gt;</SpecificationItemValues>
  </Table>
  <Table>
    <ProductCode>0987046</ProductCode>
    <SpecificationItemName>Other</SpecificationItemName>
    <SpecificationItemValues>&lt;Values xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;Value&gt;1 x PCIe 2.0 x16&lt;/Value&gt;&lt;Value&gt;2 x PCIe 2.0 x1&lt;/Value&gt;&lt;/Values&gt;</SpecificationItemValues>
  </Table>
</NewDataSet>

And this is my solution to read ProductCode, SpectificationItemName and complete value of SpecificationItemValues. 这是我读取ProductCode,SpectificationItemName和SpecificationItemValues完整值的解决方案。 Can you help me to read SpecificationItemValues by values (one by one, if more than one)? 您可以帮助我按值(一个以上,如果一个以上)读取SpecificationItemValues吗? Thank you. 谢谢。

This is my code: 这是我的代码:

foreach (XmlNode nodeSpecification in xmlDokument.SelectSingleNode("//NewDataSet"))
{
    if (nodeSpecification.Name == "Table")
    {
        foreach (XmlNode nodeElements in nodeSpecification)
        {
            if (nodeElements.Name == "ProductCode")
            {
                MessageBox.Show(nodeElements.InnerText);
            }
            if (nodeElements.Name == "SpecificationItemName")
            {
                MessageBox.Show(nodeElements.InnerText);
            }
            if (nodeElements.Name == "SpecificationItemValues")
            {
                MessageBox.Show(nodeElements.InnerText);                          
            }
        } //you were missing a closing } by the way
    }
}

Thank you @Sam.C and @Amir Sasson. 谢谢@ Sam.C和@Amir Sasson。 With your help find complete soulution. 在您的帮助下找到完整的解决方案。

if (cvorElementi.Name == "SpecificationItemValues")
{
var xmlValues = System.Net.WebUtility.HtmlDecode(cvorElementi.InnerText);
    XmlDocument valuesDoc = new XmlDocument();
    valuesDoc.LoadXml(xmlValues);

    foreach (XmlNode valuesNode in valuesDoc.SelectSingleNode("//Values"))
    {
        if (valuesNode.Name=="Value")
            {
             MessageBox.Show(valuesNode.InnerText);
            }
    }
}                               

Like this: 像这样:
on the SpecificationItemValues Node: 在SpecificationItemValues节点上:

var xmlValues = System.Web.HttpUtility.HtmlDecode(nodeElements.InnerText);
//you might want to use System.Net.WebUtility.HtmlDecode instead to avoid System.Web
XmlDocument valuesDoc = new XmlDocument();
valuesDoc .LoadXml(xmlValues );
var vals = valuesDoc.SelectNodes("//Value");
//Here You Can iterate on vals

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM