2

I've been trying to pull the value of the XML node into a string. Here is what the XML looks like:

<currentvin value="1FTWW31R08EB18119" /> 

I can't seem to figure out how to grab that value. I didn't write this XML, by the way. So far I have tried several approaches, including the following:

    public void xmlParse(string filePath)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(filePath);
        XmlNode currentVin = xml.SelectSingleNode("/currentvin");
        string xmlVin = currentVin.Value;
        Console.WriteLine(xmlVin);          
    }

Which doesn't work. I then tried:

    public void xmlParse(string filePath)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(filePath);
        string xmlVin = xml.SelectSingleNode("/currentvin").Value;
        Console.WriteLine(xmlVin);

    }

But that doesn't work either. I am getting a null reference exception stating that Object reference not set to an instance of an object. Any ideas?

1
  • Unfortunately, the XML document is proprietary, such that I cannot post the full content. Thanks for all your help! Great suggestions from everyone. I'll be escalating this to others in my department, since it looks like we'll have to solve it in house. :) Commented Oct 6, 2011 at 17:04

3 Answers 3

4

I think you're confusing the Value property of the XmlNode class, with an XML attribute named "value".

value is an attribute in your xml so either modify your xpath query to be

xml.SelectSingleNode("/currentvin/@value").Value

Or user the Attributes collection of the selected XmlNode.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe it is the attribute that I'm looking for. Sadly, this code did not help me. I'm still getting the null reference exception.
Is this all you have in your XML file, only one node? Are you sure you actually are loading the XML document? Last best is that your XPATH query is not correct.
1

You are looking for the value of the attribute "value" (that's a handful) not the value of the node itself - so you have to use the Attribute property:

string xmlVin = xml.SelectSingleNode("/currentvin").Attributes["value"].Value;

Or in the first version:

XmlNode currentVin = xml.SelectSingleNode("/currentvin");
string xmlVin = currentVin.Attributes["value"].Value;

2 Comments

These sure look like what I'm trying to get, but they did not work. I'm still getting a null reference exception.
@admiral142: Please post your full XML - if you don't even get the node you might query for it the wrong way (i.e. wrong path or your XML might use a namespace)
0

If your entire XML contains only this node then it could be xml.DocumentElement.Attributes["value"].Value;

2 Comments

There are actually several nodes in the XML, I'm just trying to get the value of the attribute "value" in the currentvin node. :P
Could you provide a full XML file example then?

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.