如何使用 C#、Linq 和 XML select 特定元素?
How to select a specific element using C#, Linq and XML?
我想从 xml 文件中读取五个黄色标记 (Screenshot) 的元素。
...
很遗憾,我没有得到结果。但我也没有错误。我做错了什么?
这是 XML 文件:
<?xml version='1.0' encoding='UTF-8'?>
<abc:ABC>
<abc:Documentation>
<abc:exporter>Exporter</abc:exporter>
</abc:Documentation>
<abc:Extension extender='Exporter v1.0'>
<plugin pluginName='Exporter' pluginVersion='1.0'/>
</abc:Extension>
<def:DEF abc:type='def:Model' abc:id='eee_1045467100313_135436_1' name='Model'>
<Element abc:type='def:Package' abc:id='32439355987_44278_14013' name='Package_'>
<abc:Extension extender='Exporter 1.0'>
<modelExtension>
<owned abc:type='def:' abc:id='848831_14014' name='Reuirement__' visibility='public' ownerOf='32439355987_44278_14013'>
<abc:Extension extender='Exporter 1.0'>
<Representation>
<daa:RepresentationObject ID='32439442709_154180_14035'>
<Contents contentHash='8c30448ee161' exporterName='Exporter' exporterVersion='1.0' abc:id='_sbrrYY9GEeicdPE2nnAHIg'>
<Objects href='#14684'/>
<Elements>_371391_15071</Elements>
</Contents>
</daa:RepresentationObject>
</Representation>
</abc:Extension>
</owned>
</modelExtension>
</abc:Extension>
<Element abc:type='def:XYZ' abc:id='14056' name='0'/>
<Element abc:type='def:XYZ' abc:id='14098' name='1a'/>
<Element abc:type='def:XYZ' abc:id='14222' name='1b'/>
<Element abc:type='def:XYZ' abc:id='14558' name='2a'/>
<Element abc:type='def:XYZ' abc:id='14600' name='2b'/>
</Element>
</def:DEF>
</abc:ABC>
这是 C# 中的代码:
XNamespace abc = "abc";
XNamespace def = "def";
XDocument doc = XDocument.Load("Le.xml");
var result = from el in doc.Elements(abc + "ABC").Elements(def + "DEF").Elements()
where el.Attribute(abc + "type").Value == "def:XYZ"
select el;
foreach (var item in result.Elements())
{
Console.WriteLine(item.Name + " = " + item.Value);
Console.WriteLine("----------");
}
在您的代码中,未声明 XmlNamespaces abc、def 和 daa。假设在正确的版本中,它们是在这样的顶级节点上声明的:
<?xml version='1.0' encoding='UTF-8'?>
<root xmlns:abc="abc" xmlns:def="def" xmlns:daa="daa">
<abc:ABC>
<abc:Documentation>
<abc:exporter>Exporter</abc:exporter>
</abc:Documentation>
<abc:Extension extender='Exporter v1.0'>
<plugin pluginName='Exporter' pluginVersion='1.0'/>
</abc:Extension>
<def:DEF abc:type='def:Model' abc:id='eee_1045467100313_135436_1' name='Model'>
<Element abc:type='def:Package' abc:id='32439355987_44278_14013' name='Package_'>
<abc:Extension extender='Exporter 1.0'>
<modelExtension>
<owned abc:type='def:' abc:id='848831_14014' name='Reuirement__' visibility='public' ownerOf='32439355987_44278_14013'>
<abc:Extension extender='Exporter 1.0'>
<Representation>
<daa:RepresentationObject ID='32439442709_154180_14035'>
<Contents contentHash='8c30448ee161' exporterName='Exporter' exporterVersion='1.0' abc:id='_sbrrYY9GEeicdPE2nnAHIg'>
<Objects href='#14684'/>
<Elements>_371391_15071</Elements>
</Contents>
</daa:RepresentationObject>
</Representation>
</abc:Extension>
</owned>
</modelExtension>
</abc:Extension>
<Element abc:type='def:XYZ' abc:id='14056' name='0'/>
<Element abc:type='def:XYZ' abc:id='14098' name='1a'/>
<Element abc:type='def:XYZ' abc:id='14222' name='1b'/>
<Element abc:type='def:XYZ' abc:id='14558' name='2a'/>
<Element abc:type='def:XYZ' abc:id='14600' name='2b'/>
</Element>
</def:DEF>
</abc:ABC>
</root>
然后你可以这样查询:
XDocument doc = XDocument.Load("Le.xml");
var result = from el in doc.Descendants(abc + "ABC")
.Elements(def + "DEF")
.Descendants("Element")
where el.Attribute(abc + "type") != null &&
(string)el.Attribute(abc + "type") == "def:XYZ"
select el;
// select new {
// Id = (int)el.Attribute(abc+"id"),
// Name = (string)el.Attribute("name")
// };
foreach (var item in result)
{
foreach (var element in item.Attributes())
{
Console.WriteLine($"{element.Name}={element.Value}");
}
Console.WriteLine("------------");
}
我想从 xml 文件中读取五个黄色标记 (Screenshot) 的元素。
...
很遗憾,我没有得到结果。但我也没有错误。我做错了什么?
这是 XML 文件:
<?xml version='1.0' encoding='UTF-8'?>
<abc:ABC>
<abc:Documentation>
<abc:exporter>Exporter</abc:exporter>
</abc:Documentation>
<abc:Extension extender='Exporter v1.0'>
<plugin pluginName='Exporter' pluginVersion='1.0'/>
</abc:Extension>
<def:DEF abc:type='def:Model' abc:id='eee_1045467100313_135436_1' name='Model'>
<Element abc:type='def:Package' abc:id='32439355987_44278_14013' name='Package_'>
<abc:Extension extender='Exporter 1.0'>
<modelExtension>
<owned abc:type='def:' abc:id='848831_14014' name='Reuirement__' visibility='public' ownerOf='32439355987_44278_14013'>
<abc:Extension extender='Exporter 1.0'>
<Representation>
<daa:RepresentationObject ID='32439442709_154180_14035'>
<Contents contentHash='8c30448ee161' exporterName='Exporter' exporterVersion='1.0' abc:id='_sbrrYY9GEeicdPE2nnAHIg'>
<Objects href='#14684'/>
<Elements>_371391_15071</Elements>
</Contents>
</daa:RepresentationObject>
</Representation>
</abc:Extension>
</owned>
</modelExtension>
</abc:Extension>
<Element abc:type='def:XYZ' abc:id='14056' name='0'/>
<Element abc:type='def:XYZ' abc:id='14098' name='1a'/>
<Element abc:type='def:XYZ' abc:id='14222' name='1b'/>
<Element abc:type='def:XYZ' abc:id='14558' name='2a'/>
<Element abc:type='def:XYZ' abc:id='14600' name='2b'/>
</Element>
</def:DEF>
</abc:ABC>
这是 C# 中的代码:
XNamespace abc = "abc";
XNamespace def = "def";
XDocument doc = XDocument.Load("Le.xml");
var result = from el in doc.Elements(abc + "ABC").Elements(def + "DEF").Elements()
where el.Attribute(abc + "type").Value == "def:XYZ"
select el;
foreach (var item in result.Elements())
{
Console.WriteLine(item.Name + " = " + item.Value);
Console.WriteLine("----------");
}
在您的代码中,未声明 XmlNamespaces abc、def 和 daa。假设在正确的版本中,它们是在这样的顶级节点上声明的:
<?xml version='1.0' encoding='UTF-8'?>
<root xmlns:abc="abc" xmlns:def="def" xmlns:daa="daa">
<abc:ABC>
<abc:Documentation>
<abc:exporter>Exporter</abc:exporter>
</abc:Documentation>
<abc:Extension extender='Exporter v1.0'>
<plugin pluginName='Exporter' pluginVersion='1.0'/>
</abc:Extension>
<def:DEF abc:type='def:Model' abc:id='eee_1045467100313_135436_1' name='Model'>
<Element abc:type='def:Package' abc:id='32439355987_44278_14013' name='Package_'>
<abc:Extension extender='Exporter 1.0'>
<modelExtension>
<owned abc:type='def:' abc:id='848831_14014' name='Reuirement__' visibility='public' ownerOf='32439355987_44278_14013'>
<abc:Extension extender='Exporter 1.0'>
<Representation>
<daa:RepresentationObject ID='32439442709_154180_14035'>
<Contents contentHash='8c30448ee161' exporterName='Exporter' exporterVersion='1.0' abc:id='_sbrrYY9GEeicdPE2nnAHIg'>
<Objects href='#14684'/>
<Elements>_371391_15071</Elements>
</Contents>
</daa:RepresentationObject>
</Representation>
</abc:Extension>
</owned>
</modelExtension>
</abc:Extension>
<Element abc:type='def:XYZ' abc:id='14056' name='0'/>
<Element abc:type='def:XYZ' abc:id='14098' name='1a'/>
<Element abc:type='def:XYZ' abc:id='14222' name='1b'/>
<Element abc:type='def:XYZ' abc:id='14558' name='2a'/>
<Element abc:type='def:XYZ' abc:id='14600' name='2b'/>
</Element>
</def:DEF>
</abc:ABC>
</root>
然后你可以这样查询:
XDocument doc = XDocument.Load("Le.xml");
var result = from el in doc.Descendants(abc + "ABC")
.Elements(def + "DEF")
.Descendants("Element")
where el.Attribute(abc + "type") != null &&
(string)el.Attribute(abc + "type") == "def:XYZ"
select el;
// select new {
// Id = (int)el.Attribute(abc+"id"),
// Name = (string)el.Attribute("name")
// };
foreach (var item in result)
{
foreach (var element in item.Attributes())
{
Console.WriteLine($"{element.Name}={element.Value}");
}
Console.WriteLine("------------");
}