[英]ChildNodes based on Parent xml Attribute in C#
This is my Xml. 这是我的Xml。
<SCat>
<S SId="1" SName="M" FName="MA">
<Cat>
<C CId="2" CName="CAS" FName="c-a" />
<C CId="3" CName="DAC" FName="d-a" />
</Cat>
</S>
<S SId="2" SName="I" FName="IA">
<Cat>
<C CId="2" CName="CAS" FName="c-a" />
<C CId="3" CName="DAC" FName="d-a" />
</Cat>
</S>
<S SId="3" SName="D" FName="DA">
<Cat>
<C CId="2" CName="CAS" FName="c-a" />
<C CId="3" CName="DAC" FName="d-a" />
</Cat>
</S>
</SCat>
I wrote this code. 我写了这段代码。
int Scode = 1;
dsS = new DataSet();
dsS.ReadXml(HttpContext.Current.Server.MapPath(Path));
This is where i am stucked. 这就是我卡住的地方。 I want to get all "Cat" in the Datatable which has attribute "SId" = 1.
我想在具有属性“ SId” = 1的数据表中获取所有“猫”。
Thanks 谢谢
使用下面的XPATH,您将获得SId ='1'
所有子节点
/SCat/S[@SId='1']/Cat
You should use XmlDocument object to load the entire document and then provide the node path and attribute selector. 您应该使用XmlDocument对象加载整个文档,然后提供节点路径和属性选择器。 It's all well documented at MSDN ( link1 ).
所有这些都在MSDN( link1 )上有详细记录。 Following is a code snippet from that site:
以下是该站点的代码片段:
Listing 1. 清单1。
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
XmlNode root = doc.FirstChild;
//Create a new attribute.
string ns = root.GetNamespaceOfPrefix("bk");
XmlNode attr = doc.CreateNode(XmlNodeType.Attribute, "genre", ns);
attr.Value = "novel";
//Add the attribute to the document.
root.Attributes.SetNamedItem(attr);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Listing 2 ( link2 ) 清单2( link2 )
XElement root = XElement.Load("PurchaseOrder.xml");
IEnumerable<XElement> address =
from el in root.Elements("Address")
where (string)el.Attribute("Type") == "Billing"
select el;
foreach (XElement el in address)
Console.WriteLine(el);
Hope this will help. 希望这会有所帮助。 Regards, AB
问候,AB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.