Click here to Skip to main content
15,436,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a XML file and it goes as below and I'm trying to bring out the Node Attribute value from Node Value




<Roots>
    <Root id="XYZ">


        <Select val = "SELECT0">
            <Pin val = "00"> A </Pin>
            <Pin val = "01"> B </Pin>
            <Pin val = "02"> C </Pin>
            <Pin val = "03"> D </Pin>

        </Select>

        <Select val = "SELECT1">
            <Pin val = "00"> E </Pin>
            <Pin val = "01"> F </Pin>
            <Pin val = "02"> G </Pin>
            <Pin val = "03"> H </Pin>

        </Select>
    </Root>
</Roots>


What I have tried:

I have tried this way and able to get all the Pin values from all Select, but I do not have an idea on how to provide the Pin values as input and get Select value as output

XmlDocument readXmlDoc = new XmlDocument();
readXmlDoc.Load("load.xml");

string Pin = " ";

 XmlNodeList xnList = readXmlDoc.SelectNodes("Roots/Root/Select/Pin");
 foreach (XmlNode xn in xnList)
 {

     Pin = xn.InnerText;
     Console.WriteLine("Name is: " + Pin);
 }
Posted
Updated 29-Mar-22 22:34pm
v8

Seems simple enough using LINQ to XML[^]:
C#
// Values to find:
string pin0 = " value0 ", pin1 = null, pin2 = " value2 ", pin3 = null;

XDocument document = XDocument.Load("load.xml");
var select = doc.Descendants("Select").Select(s => new
{
    val = (string)s.Attribute("val"),
    pins = s.Elements("Pin").ToDictionary(p => (int)p.Attribute("val"), p => (string)p),
});

if (pin0 != null) 
{
    select = select.Where(s => s.pins.TryGetValue(0, out var p0) && p0 == pin0);
}
if (pin1 != null) 
{
    select = select.Where(s => s.pins.TryGetValue(1, out var p1) && p1 == pin1);
}
if (pin2 != null) 
{
    select = select.Where(s => s.pins.TryGetValue(2, out var p2) && p2 == pin2);
}
if (pin3 != null) 
{
    select = select.Where(s => s.pins.TryGetValue(3, out var p3) && p3 == pin3);
}

/*
Output:

{
    val: "SELECT1",
    pins: 
    {
        [0] = " value0 ",
        [1] = " value1 ",
        [2] = " value2 ",
        [3] = " value3 ",
    }
}
*/
NB: The values need to match exactly. Your source XML document has spaces around the values, so the filter values need to have spaces around the values. If you want to ignore the spaces, you'll need to trim the values from the XML document.
 
Share this answer
 
Comments
Maciej Los 29-Mar-22 15:49pm    
5ed!
Maybe if you try something like this:

public string GetSelectValue(string pinValue)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("load.xml"));
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("Select");
string parentName = string.Empty;
foreach (XmlNode node in nodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
string label = node.ChildNodes[i].InnerXml;
if (label == pinValue)
{
parentName = node.Attributes["val"].Value;
break;
}
}
}
return parentName;
}
 
Share this answer
 
Comments
enoughIsenough 28-Mar-22 1:59am    
Hi, in this case the code is not entering into the foreach block

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900