简体   繁体   中英

Get child node value from xml using xpath

    <Demo_Test>
      <Step ID="1">
        <ACTION>Point</ACTION>
        <CLASS_ID>dfsfsdf</CLASS_ID>
      </Step>
    <Step ID="2">
        <ACTION>Point</ACTION>
        <CLASS_ID>Avkddd</CLASS_ID>
      </Step>
    <Step ID="3">
        <ACTION>Point</ACTION>
        <CLASS_ID>afsasfa</CLASS_ID>
      </Step>
   <Step ID="4">
        <ACTION>SubAction</ACTION>
        <CLASS_ID>afsasfa</CLASS_ID>
      </Step>
    </Demo_Test>

I want to modify value of "CLASS_ID" having "Action" node value is "Point" For that i have written below code but doesn't work

 string l_xPath = "//Step/ACTION["Point"]";
 XmlNodeList l_nodeList = l_doc.SelectNodes(l_xPath);

Using xml linq I put results into a dictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<int, string> dict = doc.Descendants("Step")
                .Where(x => (string)x.Element("ACTION") == "Point")
                .GroupBy(x => (int)x.Attribute("ID"), y => (string)y.Element("CLASS_ID"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }

    }

}

You can use this:

        var l_nodeList = l_doc.SelectNodes("//Step[ACTION='Point']");
        foreach (XmlNode item in l_nodeList)
        {
            Console.WriteLine(item.SelectSingleNode("CLASS_ID").InnerText);
        }

But I like jdweng's answer better

at first you need an object like this:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="Step")]
    public class Step {
        [XmlElement(ElementName="ACTION")]
        public string ACTION { get; set; }
        [XmlElement(ElementName="CLASS_ID")]
        public string CLASS_ID { get; set; }
        [XmlAttribute(AttributeName="ID")]
        public string ID { get; set; }
    }

    [XmlRoot(ElementName="Demo_Test")]
    public class Demo_Test {
        [XmlElement(ElementName="Step")]
        public List<Step> Step { get; set; }
    }

}

then you can deserilze xml with this method:

public static T XmlDeserializer<T>(string xmlString)
{
    var instance = default(T);
    var xmlSerializer = new XmlSerializer(typeof(T));
    using (var stringreader = new StringReader(xmlString))
        instance = (T)xmlSerializer.Deserialize(stringreader);

    return instance;
}

then just use it like this:

var result = XmlDeserializer<Demo_Test >("your xml string here");
var l_nodeList =result.Step[0].CLASS_ID;

This solution achieved using XPath

XmlDocument d = new XmlDocument();
            d.Load(@"C:\Users\Desktop\test.xml");

            XPathNavigator nav = d.CreateNavigator();
            XPathExpression exp;
            exp = nav.Compile("//Step[ACTION='Point']");
            XPathNodeIterator iterator = nav.Select(exp);
            var a = d.SelectNodes("//Step[ACTION='Point']");

            while (iterator.MoveNext())
            {
                Console.WriteLine(iterator.Current.SelectSingleNode("CLASS_ID").Value);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM