0

This is given 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. Can you help me to read SpecificationItemValues by values (one by one, if more than one)? 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
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Marco
  • 11
  • 1
  • 2
    you will have to Xml Decode the inner text value of "SpecificationItemValues" and parse through them using an other instance of xml document. this answer should give you an idea http://stackoverflow.com/questions/6757019/how-to-decode-string-to-xml-string-in-c-sharp – Sam.C Jun 23 '16 at 19:25

2 Answers2

1

Thank you @Sam.C and @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);
            }
    }
}                               
Marco
  • 11
  • 1
  • Marco,you can mark our answers as useful so others can benefit. – Amir Sasson Jun 26 '16 at 21:18
  • @AmirSasson I really would like to and I try few times, but as I'm new here Stackoverflow don't let me :-( Sorry! Something like 15 reputation... – Marco Jun 28 '16 at 06:14
0

Like this:
on the SpecificationItemValues Node:

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
Amir Sasson
  • 10,985
  • 1
  • 13
  • 25