[英]Parsing a section of XML
I have a section of XML that I am trying to pull out a name from. 我有一部分XML试图从中提取名称。 the XML looks like this:
XML看起来像这样:
here is the xml layout.
<point href="/software/point/av/371/">
<obj is="testdata/">
<bool val="true" name="eventDetectionEnable" />
<real val="-500.6300048828125" name="minimumValue" />
<int val="0" name="controlClass" />
<int val="1490" name="revision" />
<real val="0" name="highLimit" />
<int val="6" name="dimensionality" />
<reltime val="PT0S" name="heartbeatInterval" />
<enum val="event" name="notifyType" />
<str val="Verticle Spread Pressure" name="name" />
<real val="0" name="deadband" />
<real val="0" name="lowLimit" />
<str val="" name="protocolID" />
<str val="" name="description" />
<reltime val="PT0S" name="eventTimeDelay" />
<real val="1" name="covIncrement" />
<real val="7.9999995231628418" name="relinquishDefault" />
<op name="notifyNow" />
<real val="500.6300048828125" name="maximumValue" />
</obj>
</point>
I need to pull out the name from line 我需要从行中拔出名字
<str val="Verticle Spread Pressure" name="name" />
I can get a list of all the nodes and read the href tag, but nothing below 我可以获取所有节点的列表并读取href标记,但下面没有任何内容
var data = OpenPLCfile(ofd.FileName, "pointconfig.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(data);
string xpath = "PointsConfiguration/SoftwarePoints/point";
var nodes = xmlDoc.SelectNodes(xpath);
foreach (XmlNode childrenNode in nodes)
{
string pointID = childrenNode.Attributes["href"].Value;
Console.WriteLine(pointID)
}
You can apply an XPath
expression on the XmlNode
via the SelectSingleNode
method. 您可以通过
SelectSingleNode
方法在XmlNode
上应用XPath
表达式。
XmlNode nameNode = childrenNode.SelectSingleNode("obj/str[@name='name']/@val");
string name = nameNode.Value;
Full example 完整的例子
var data = OpenPLCfile(ofd.FileName, "pointconfig.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(data);
string xpath = "PointsConfiguration/SoftwarePoints/point";
var nodes = xmlDoc.SelectNodes(xpath);
foreach (XmlNode childrenNode in nodes)
{
string pointID = childrenNode.Attributes["href"].Value;
Console.WriteLine(pointID)
XmlNode nameNode = childrenNode.SelectSingleNode("obj/str[@name='name']/@val");
string name = nameNode.Value;
Console.WriteLine(name);
}
If you only want to get the value of name attribute from the element str where value of val attribute is "Verticle Spread Pressure", then here is the solution: 如果只想从元素str获得name属性的值,其中val属性的值是“垂直扩展压力”,那么这里是解决方案:
var data = OpenPLCfile(ofd.FileName, "pointconfig.xml");
XDocument doc = XDocument.Parse(data);
var strElements = doc.Descendants("str");
var targetElement = strElements.Where(x => x.Attribute("val") != null && x.Attribute("val").Value == "Verticle Spread Pressure").FirstOrDefault();
if (targetElement != null)
{
string name = targetElement.Attribute("name").Value;
}
System.Xml.Linq is the namespace for XDocument System.Xml.Linq是XDocument的命名空间
Using Xml Linq you can get a dictionary of all named items 使用Xml Linq,您可以获得所有命名项的字典
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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<string, string> dict = doc.Descendants("obj").FirstOrDefault().Elements()
.GroupBy(x => (string)x.Attribute("name"), y => (string)y.Attribute("val"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.