Reputation: 11
<physicalResource>
<prName>PRS_EID</prName>
<prDescription>PRS_EID</prDescription>
<physicalResourceCharacteristic>
<characteristic>
<name>eidno</name>
<value>SH001499000</value>
</characteristic>
<characteristic>
<name>flatno</name>
<value>14303</value>
</characteristic>
</physicalResourceCharacteristic>
</physicalResource>
<physicalResource>
<prName>PRS_OLT</prName>
<prDescription>PRS_OLT</prDescription>
<physicalResourceCharacteristic>
<characteristic>
<name>device</name>
<value>WC-OMU-AO01</value>
</characteristic>
<characteristic>
<name>frame</name>
<value>1</value>
</characteristic>
<characteristic>
<name>port</name>
<value>5</value>
</characteristic>
</physicalResourceCharacteristic>
</physicalResource>
Hello Dears.. I have an xml file. It contains different nodes with same node name. In the example under physicalResource node, I want to extract prName and all characteristic's name and value. But I cant parse them seperately. I'm using
nodeListPrs = root.SelectNodes("/physicalResource/physicalResourceCharacteristic/characteristic", nsmgr);
It extracts all charactics value for both nodes. How can i extract these parameters from single physicalResource node?
Upvotes: 1
Views: 2055
Reputation: 11
Thanks all. I have resolved by using SelectSingleNode and SelectNodes cascade:
XmlNodeList nodeListPrs = root.SelectNodes("/ns2:serviceOrder/resourceFacingService/physicalResource", nsmgr);
foreach (XmlNode book in nodeListPrs)
{
string prsName = book["prName"].InnerText;
try
{
nodeListPrsCh = book.SelectSingleNode("physicalResourceCharacteristic").SelectNodes("characteristic");
foreach (XmlNode characteristics in nodeListPrsCh)
{
dataGridView3.Rows.Add();
dataGridView3.Rows[i].Cells[0].Value = prsName;
try { string name = characteristics["name"].InnerText; dataGridView3.Rows[i].Cells[1].Value = name; }
catch { dataGridView3.Rows[i].Cells[1].Value = "-"; }
try { string value = characteristics["value"].InnerText; dataGridView3.Rows[i].Cells[2].Value = value; }
catch { dataGridView3.Rows[i].Cells[2].Value = "-"; }
i = i + 1;
}
}
catch
{
dataGridView3.Rows.Add();
dataGridView3.Rows[i].Cells[0].Value = prsName;
dataGridView3.Rows[i].Cells[1].Value = "-";
dataGridView3.Rows[i].Cells[2].Value = "-";
i = i + 1;
}
}
Upvotes: 0
Reputation: 29022
To select all prName
nodes with the corresponding characteristic
nodes of the first physicalResourceCharacteristic
node, you would use the follwing XPath expression in SelectNodes
.
/res/physicalResource[1]/physicalResourceCharacteristic/characteristic | /res/physicalResource[1]/prName
The result is
<?xml version="1.0" encoding="UTF-8"?>
<prName>PRS_EID</prName>
<characteristic>
<name>eidno</name>
<value>SH001499000</value>
</characteristic>
<characteristic>
<name>flatno</name>
<value>14303</value>
</characteristic>
I'm not sure if this is what you strive for. You could iterate over the count of physicalResource
constructing [xxx]
XPath-expressions to get a nodeset with a couple of these entries. Or you could omit the [1]
to get a nodeset with all physicalResource
s prefixed by prName
s, but with mixed node-types.
Upvotes: 0
Reputation: 171
You can use xmldocument and load that xml to xmldocument then you can use selectsinglenode. It would help!!
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.DocumentElement.SelectSingleNode(path for the node..);
Upvotes: 1
Reputation: 34421
Use XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication78
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("physicalResource").Select(x => new {
prName = (string)x.Element("prName"),
characteristics = x.Descendants("characteristic").Select(y => new {
name = (string)y.Element("name"),
value = (string)y.Element("value")
}).ToList()
}).ToList();
}
}
}
Upvotes: 0