0

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

5 Answers 5

2

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

string widgetCodeName = ManifestDocument.Root.Attribute("SymbolicName").Value;
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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

3 Comments

And how can i select the Path attribute in the element Assembly?
ManifestDocument.Root.Attribute("SymbolicName").Value is actually much better than casting it explicitly.
@bLaXjack no, casting is better because .Value will throw an exception if the attirubute wasn't found
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.