Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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());
}
 
Share this answer
 
Comments
Maciej Los 10-Sep-19 5:53am    
;)
I prefer to use XDocument class[^] and Linq.

See:
C#
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!
 
Share this answer
 
Comments
RickZeeland 10-Sep-19 5:02am    
Good one, 5d :)
Maciej Los 10-Sep-19 5:52am    
Thank you, Rick.
Member 10738387 13-Sep-19 0:37am    
yeah its working... thanks Rick :)
Maciej Los 13-Sep-19 2:12am    
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900