0

I'm not sure what I am missing here.. I have an XML file which has several nodes.. in particular I am trying to get the LocalName of the node which is descendant of "Requirement". I've tried every combination I can think of and can't get it to let me access the node. When I check for if Requirement has elements, I get true... when I check to see if it has descendents - I get false.

Here is the XML

           <Requirement type="Level"><gt>11</gt></Requirement>

Edit @ Jon - My question is how do I access the "GT" node? (It can change to be other items, so I do not want to reference it directly).

Here is my code so far:

        public override void LoadXml(XElement element)
    {
        Value = element.Value;

        EquateType = element.LastNode.Parent.Name.LocalName;
    }

EquateType is the field I am trying to modify... LastNode actually returns an error, but as I said above I was navigating in the Immediate window and cannot seem to find the path that I need. Thanks in advance!

0

4 Answers 4

1

Or you can use XPAth..

EDIT:

XmlNode node=  doc.SelectSingleNode("/Requirement//*"); //returns first occurence

string name = node.Name; //tada!

XmlNodeList list = doc.SelectNodes("/Requirement//gt"); //selects aoccuring anywhere within Requirement.

EDIT2: To convert from XELement to XmlNode you can..

Step1: Create an XmlReader using the CreateReader() method.

Step2: Then load an XmlDocument back using the XmlReader returned from CreateReader().

Step3: Return the XmlDocument as XMlNode since XmlDocument inherits XmlNode. :)

Your code should like something below..

public static XmlNode GetXmlNode(this XElement element)
{
    using (XmlReader xmlReader = element.CreateReader())
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlReader);
        return xmlDoc;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

the problem is sometimes GT will not be there.. gt could be "lt" or "lteq" etc...
@Stacey: Then replace gt with * and / instead of // as,, doc.SelectNodes("/Requirement/*"). It selects all immediate descendant nodes of Requirement.
XPath would be my choice as well for selecting nodes from an XML Document.
@conqenator This method may require me to re-write a lot of code. The method that uses this inherts from an abstract class - so all of the other classes that inherit from it have to be re-written. I tried to cast my XElement as an XmlNode but it wont let me. Is there another way to convert an Xelement to an XmlNode without changing the type that is passed into the method?
@Stacey: Yeah, I understand that can be quite a show-stopper. Well, I have added the code to convert from XELement to XmlNode. Hope that helps.
|
0

try this

var items = doc.Descendants("Requirement")
               .Where(node => (string)node.Name == "gt")
               .Select(node => node.Value.ToString())
               .ToList();

4 Comments

Hi Jux - I am trying to get the node "Gt" without referencing "gt" as the node name can change.
If you want to get the first descendant: doc.Descendants("Requirement").First().Select(n => n.Value.ToString())
I do not have a "First()" But I do have a FirstNode() which seems to point to the GT node. The problem is, how do I get the name of the gt node? I know in other cases I have used .Name.LocalName(). I think some confusion is that I do not have access to the entire document right now, I am just looking at the piece that is referenced above as it is passed into a specific class and parsed out for the info I need.
@Stacey:I have updated my answer. Does that solve the above problem?
0

If you're using an XDocument, the first element in the document that's under a Requirement element is:

XElement e = d.Descendants("Requirement").Elements().First();

and you can access its LocalName property to get its local name. (Note the use of Elements, which returns immediate child elements, rather than Descendants, which returns all descendant elements. It doesn't matter in this case, since the first child element and the first descendant element are the same thing, but indiscriminately using Descendants when you're only looking for child elements can get you into trouble.)

If you want to use XPath, you can:

XElement e = d.XPathSelectElement("//Requirement/*");

2 Comments

Iam working with an Xelement that is being passed into the method. I do noth ave XPathSelectElement (I'm assuming this is only part of Xdocument. Is there a way around this?
Don't assume, look it up. XPathSelectElement is an extension method that works on any XNode. And, of course, you can always use XElement.Document to get to the document that owns the element.
0

a huge thanks for everyone and their help. I ended up being able to get this to work using Linq.

            EquateType = element.Elements().First().Name.LocalName;

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.