从XML元素属性选择 [英] Select attribute from xml element
本文介绍了从XML元素属性选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的根节点选择属性,但我不断收到关于选择部分空例外。
什么是获得价值的正确方法?我的属性。
我想获得的属性值的值:SymbolicName
XML文档
<捆绑的xmlns =金塔:uiosp束舱单-2.0NAME =ContactUsPluginSymbolicName =ContactUsPlugin 版本=1InitializedState =主动>
<激活类型=ContactUsPlugin.Activator策略=立即/>
<&运行GT;
<大会PATH =bin\ContactUsPlugin.dll共享=假/>
< /运行>
<官能度≥2;
<控制器>关于< /控制器>
<视图>指数 - LT; /视图>
< /功能>
<脚本和GT;
<脚本版本=1>
<地点> E:\Git Projects\Kapsters\Plugins\ContactUsPlugin\Sql\Sql1.txt< /地点>
< / SCRIPT>
<脚本版本=2>
<地点> E:\Git Projects\Kapsters\Plugins\ContactUsPlugin\Sql\Sql1.txt< /地点>
< / SCRIPT>
< /脚本>
< /包>
我试过:
字符串widgetCodeName =
(从数据库中ManifestDocument.Elements()选择db.Attribute(SymbolicName)值。)。首先();
串widgetCodeName =
(从数据库中ManifestDocument.Descendants()选择db.Element(捆绑)属性(SymbolicName)值。)。首先();
串widgetCodeName =
(从数据库中ManifestDocument.Element(捆绑)属性(),其中db.Name ==SymbolicName选择db.Value)。首先();
解决方案
所有这些例子中使用取决于如果你只需要值或XAttribute本身:
的XDocument ManifestDocument = XDocument.Load(YourXmlFile.xml);
VAR更改为MyQuery = ManifestDocument.Elements()属性(SymbolicName)第一(); //将XAttribute
串myvalue的= ManifestDocument.Root.Attribute( SymbolicName)值; //本身的价值
VAR secondquery = ManifestDocument.Descendants()属性(SymbolicName)第一(); //另一种方式来获得XAttribute
最后一个(secondquery)将获得SymbolicName属性,即使在另一个节点还定义如果删除了。首先( )。
I'm trying to select an attribute from my root node but i keep getting a null exception on the select part.
What's the correct way of getting the value of my attribute?
The value i am trying to get value of the attribute: SymbolicName
The xml document:
<Bundle xmlns="urn:uiosp-bundle-manifest-2.0" Name="ContactUsPlugin" SymbolicName="ContactUsPlugin" Version="1" InitializedState="Active">
<Activator Type="ContactUsPlugin.Activator" Policy="Immediate" />
<Runtime>
<Assembly Path="bin\ContactUsPlugin.dll" Share="false" />
</Runtime>
<Functionality>
<Controller>About</Controller>
<View>Index</View>
</Functionality>
<Scripts>
<Script version="1">
<Location>E:\Git Projects\Kapsters\Plugins\ContactUsPlugin\Sql\Sql1.txt</Location>
</Script>
<Script version="2">
<Location>E:\Git Projects\Kapsters\Plugins\ContactUsPlugin\Sql\Sql1.txt</Location>
</Script>
</Scripts>
</Bundle>
I tried:
string widgetCodeName =
(from db in ManifestDocument.Elements() select db.Attribute("SymbolicName").Value).First();
string widgetCodeName =
(from db in ManifestDocument.Descendants() select db.Element("Bundle").Attribute("SymbolicName").Value).First();
string widgetCodeName =
(from db in ManifestDocument.Element("Bundle").Attributes() where db.Name == "SymbolicName" select db.Value).First();
解决方案
All this examples work depending on if you need only the value or the XAttribute itself:
XDocument ManifestDocument = XDocument.Load("YourXmlFile.xml");
var myquery = ManifestDocument.Elements().Attributes("SymbolicName").First();//the XAttribute
string myvalue = ManifestDocument.Root.Attribute("SymbolicName").Value;//the value itself
var secondquery = ManifestDocument.Descendants().Attributes("SymbolicName").First();//another way to get the XAttribute
The last one (secondquery) will get SymbolicName attribute even if also defined in another node if you remove the .First().
这篇关于从XML元素属性选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文