[英]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 我想修改具有“动作”节点值为“点”的“ CLASS_ID”的值,为此,我编写了以下代码,但不起作用
string l_xPath = "//Step/ACTION["Point"]";
XmlNodeList l_nodeList = l_doc.SelectNodes(l_xPath);
Using xml linq I put results into a dictionary 使用xml linq,我将结果放入字典中
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 但我更喜欢jdweng的答案
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: 那么您可以使用以下方法对xml进行反序列化:
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 使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.