C# 从xml元素中选择属性
我试图从根节点选择一个属性,但我一直在选择部分得到一个null异常 获取属性值的正确方法是什么 我试图获取属性值的值:SymbolicName xml文档:C# 从xml元素中选择属性,c#,linq-to-xml,C#,Linq To Xml,我试图从根节点选择一个属性,但我一直在选择部分得到一个null异常 获取属性值的正确方法是什么 我试图获取属性值的值:SymbolicName xml文档: <Bundle xmlns="urn:uiosp-bundle-manifest-2.0" Name="ContactUsPlugin" SymbolicName="ContactUsPlugin" Version="1" InitializedState="Active"> <Activator Type="Cont
<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>
您的
Bundle
元素有一个xml名称空间。您需要指定它:
XNamespace ns = "urn:uiosp-bundle-manifest-2.0";
string widgetCodeName = (string)ManifestDocument
.Element(ns + "Bundle")
.Attribute("SymbolicName");
或者,如果Bundle
是您的根元素,您可以执行以下操作:
string widgetCodeName = (string)ManifestDocument
.Root
.Attribute("SymbolicName");
您的Bundle
元素有一个xml名称空间。您需要指定它:
XNamespace ns = "urn:uiosp-bundle-manifest-2.0";
string widgetCodeName = (string)ManifestDocument
.Element(ns + "Bundle")
.Attribute("SymbolicName");
或者,如果Bundle
是您的根元素,您可以执行以下操作:
string widgetCodeName = (string)ManifestDocument
.Root
.Attribute("SymbolicName");
如果这是您的整个XML,那么您可以使用下面的代码获得它
XElement elem = XElement.Parse(xmlStr);
string val = elem.Attribute("SymbolicName").Value;
其中xmlStr是您的XML。如果缺少属性,则attribute方法将返回null,因此请确保在访问Value属性之前测试null,如果这是您的整个XML,则可以使用下面的代码获得它
XElement elem = XElement.Parse(xmlStr);
string val = elem.Attribute("SymbolicName").Value;
其中xmlStr是您的XML。如果缺少属性,则attribute方法将返回null,因此确保在访问Value属性之前测试null,根据您拥有的xml,bundle标记是根节点。尝试:
string widgetCodeName = ManifestDocument.Root.Attribute("SymbolicName").Value;
根据您拥有的xml,bundle标记是根节点。尝试:
string widgetCodeName = ManifestDocument.Root.Attribute("SymbolicName").Value;
所有这些示例都取决于您是否只需要值或XAttribute本身:
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
如果删除.First(),最后一个(secondquery)将获得SymbolicName属性,即使在另一个节点中也定义了该属性。所有这些示例都取决于您是否只需要值或XAttribute本身:
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
如果删除.First(),即使在另一个节点中也定义了SymbolicName属性,最后一个(secondquery)也将获得SymbolicName属性。尝试以下操作:
XNamespace ns = "urn:uiosp-bundle-manifest-2.0";
XDocument xd = XDocument.Load(@"xmlDocument");
var assemblyLocation = from a in xd.Descendants(ns + "Bundle")
select new
{
Path = a.Element(ns + "Runtime").Element(ns + "Assembly").Attribute("Path").Value,
};
试试这个:
XNamespace ns = "urn:uiosp-bundle-manifest-2.0";
XDocument xd = XDocument.Load(@"xmlDocument");
var assemblyLocation = from a in xd.Descendants(ns + "Bundle")
select new
{
Path = a.Element(ns + "Runtime").Element(ns + "Assembly").Attribute("Path").Value,
};
如何在元素程序集中选择路径属性?ManifestDocument.Root.attribute(“SymbolicName”)。值实际上比显式转换要好得多。@bLaXjack否,强制转换更好,因为.Value
如果找不到AttirBute,将引发异常,如何在元素程序集中选择Path属性?ManifestDocument.Root.attribute(“SymbolicName”)。值实际上比显式强制转换好得多。@bLaXjack否,强制转换更好,因为如果找不到AttirBute,.Value
将引发异常