Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Diag_Configuaration>
<Standard_Error>
<stopmode>
<value1>FreeWheel</value1>
<value2>Manual/AFR</value2>
</stopmode>
<priority>
<value1>Normal</value1>
<value2>High</value2>
</priority>
<ResetMethod>
<value1>Manual</value1>
<value2>Manual/AFR</value2>
</ResetMethod>
</Standard_Error>
</Diag_Configuaration>


i have tried using Xpath to read the values but it has only particular child node values, sometimes innertext has combined values like " NormalHigh" how can be the xml and its child node's values will convert to List. in the above XML i need to create three list of 1. stopmode, 2.priority, 3. Resetmethod. the innertext should be values in each respective list.

What I have tried:

XmlDocument doc = new XmlDocument();
                doc.Load("C:\\TestDataBaseManager\\Diag_Congiuration.xml");

                XmlNodeList stopmode = doc.SelectNodes("/Diag_Configuaration/Standard_Error/stopmode");

                foreach(XmlNode node in stopmode)
                {                    
                    StopMode.Items.Add(node.InnerText);
                }

                XmlNodeList priority = doc.SelectNodes("/Diag_Configuaration/Standard_Error/priority");

                foreach (XmlNode node in priority)
                {
                    Priority.Items.Add(node.InnerText);
                }

                XmlNodeList resetmethod = doc.SelectNodes("/Diag_Configuaration/Standard_Error/ResetMethod");

                foreach (XmlNode node in resetmethod)
                {
                    ResetMethod.Items.Add(node.InnerText);
                }
Posted
Updated 9-Sep-19 22:57pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

var xmlSource = XElement.Load(C:\\TestDataBaseManager\\Diag_Congiuration.xml");
var xList = xmlSource.Elements(@"Standard_Error");
List<string> stopList = new List<string>();

foreach (var x in xList)
{
    if (x == null)
    {
        continue;
    }

    var stopmode = x.Element("stopmode");
    var value1 = stopmode.Element("value1");
    var value2 = stopmode.Element("value2");
    stopList.Add(value1.Value.ToString());
    stopList.Add(value2.Value.ToString());
}
   
Comments
Maciej Los 10-Sep-19 5:53am
   
;)
Rate this:
Please Sign up or sign in to vote.

Solution 2

I prefer to use XDocument class[^] and Linq.

See:
XDocument xdoc = XDocument.Load("FullFileName");

List<string> stopmodelist = xdoc.Descendants("stopmode").Descendants()
	.Where(x=>x.Name.ToString().StartsWith("value"))
	.Select(x=>x.Value)
	.ToList();


Returns:
FreeWheel 
Manual/AFR


Good luck!
   
Comments
RickZeeland 10-Sep-19 5:02am
   
Good one, 5d :)
Maciej Los 10-Sep-19 5:52am
   
Thank you, Rick.
Member 10738387 3 days ago
   
yeah its working... thanks Rick :)
Maciej Los 3 days ago
   
You're very welcome.
Cheers
Maciej

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100