Jamie
Jamie

Reputation: 3051

Select attribute from xml element

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();

Upvotes: 0

Views: 329

Answers (5)

Thirisangu Ramanathan
Thirisangu Ramanathan

Reputation: 614

Try this :

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,
                               };

Upvotes: 0

terrybozzio
terrybozzio

Reputation: 4542

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().

Upvotes: 1

Yuan Shing Kong
Yuan Shing Kong

Reputation: 674

According to the xml that you have, the bundle tag is the root node. Try:

string widgetCodeName = ManifestDocument.Root.Attribute("SymbolicName").Value;

Upvotes: 2

flo_badea
flo_badea

Reputation: 784

if this is your entire XML then you can get it with the code below.

XElement elem = XElement.Parse(xmlStr);
string val = elem.Attribute("SymbolicName").Value;

where xmlStr is your XML. if the attribute is missing then the Attribute method will return null so make sure you test for null before accessing the Value property

Upvotes: 1

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

Your Bundle element has a xml namespace. You need to specify it:

XNamespace ns = "urn:uiosp-bundle-manifest-2.0";

string widgetCodeName = (string)ManifestDocument
                          .Element(ns + "Bundle")
                          .Attribute("SymbolicName");

Or, if Bundle is your Root element you can do:

string widgetCodeName = (string)ManifestDocument
                          .Root
                          .Attribute("SymbolicName");

Upvotes: 0

Related Questions