我需要获取XML父标记属性

时间:2013-07-28 04:07:39

标签: c# xml linq

此代码使用属性获取xml标记的值,我需要反转此操作并使用xml的标记名称获取属性值。

XElement main = XElement.Load(fi.FullName);

//Linq query for searching Ports address by ID Attributes
IEnumerable<XElement> searched =
from ports in main.XPathSelectElements("Network/Ports")
where (string)ports.Attribute("id") == fi.Name.Substring(0,36)
select ports;

这不起作用,但它应该是这个程序的东西我试图获得标签名称匹配的属性。

//Something more like this
IEnumerable<XElement> searchedat =
                from netatt in main.FirstAttribute = "id"
                where netatt.Name == "Network"
                select netatt;

2 个答案:

答案 0 :(得分:0)

var xDoc = XDocument.Parse("XML String");
var attributeValue = xDoc.Root.Element("Name of the element").Attribute("Name of attribute").Value

您可能需要根据XML结构进一步解析xml树

答案 1 :(得分:0)

IEnumerable<XElement> searched =
from ports in main.XPathSelectElements("Network/Ports")
where ports.LocalName == "TagNameToCompareHere"
select ports.Attribute("id").Value;