1

I'm developing my first WPF application with C# but I have a problem when I'm trying to read a Xml attribute.

I have the following Xml:

<?xml version="1.0" encoding="utf-8"?>
<Dictionary EnglishName="Italian" CultureName="Italian" Culture="">
    <!-- MainWindow -->
    <Value ID="WpfApplication1.MainWindow.BtnDrawCircle" Content="Circonferenza"/>
    <Value ID="WpfApplication1.MainWindow.BtnDrawLine" Content="Linea"/>
    ....
    ....
</Dictionary>`

Now I try to get the Attribute "Content" with the following method:

public static string ReadNodeAttribute(string IDAttribute)
    {
        try
        {
            XmlDocument _Doc = new XmlDocument();
            _Doc.Load("myPath\\Language\\it-IT.xml");

            string _Value = _Doc.SelectSingleNode("//Value[@ID=" + IDAttribute + "]").Attributes["Content"].Value.ToString();

            return _Value;
        }
        catch (Exception ex)
        {
            return null;
        }
}

But it doesn't work:

Error : ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}

4
  • 1
    "it dosn't work". Can you be more spesific about what dosnt work exactly? What exactly are you passing for IDAttribute? Commented Jun 28, 2016 at 13:49
  • To IDAttribute I try to pass this "WpfApplication1.MainWindow.BtnDrawCircle".... but I receive the follwing Error : ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException} Commented Jun 28, 2016 at 14:01
  • The way to do this stuff isn't to write a big wodge of code with multiple points of failure, and then try to guess where it failed. Build it up one little piece at a time. First, try var xn = _Doc.SelectSingleNode("//Value[@ID=" + IDAttribute + "]"); and see what you get for xn. You can use the watch window in the debugger. Once you figure out the right xpath expression to get the node you want, you can look at its Attributes collection in the debugger and see what is in there. For all I know, the relative path to your file may be wrong. Check _Doc after Load! Commented Jun 28, 2016 at 14:10
  • In case you will try to get "IDAttribute" that doesn't exist in the xml you will get this error, that's why in my answer I added it. Commented Jun 28, 2016 at 14:19

2 Answers 2

1

You got

null reference exception

because you didn't check for null in case your IDAttribute doesn't exist in the XML.

Just change to your path and it will work.

using System;
using System.Linq;
using System.Xml.Linq;

  public static string ReadNodeAttribute(string IDAttribute)
        {
            string _Value = "";
            try
            {
               //I used System.IO.Path.GetFullPath because I tried it with ConsoleApplication.
               //Use what ever work for you to load the xml.
                XDocument xdoc = XDocument.Load(System.IO.Path.GetFullPath("XMLFile1.xml"));
                var myValue = xdoc.Descendants("Value").FirstOrDefault(i => i.Attribute("ID").Value == IDAttribute);
                if (myValue != null)
                {
                    _Value = myValue.Attribute("Content").Value;

                    return _Value;
                }
            }
            catch (Exception ex)
            {
                return null;
            }

            return _Value;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

It Work Thank you very much
1

I have tried using Linq to Xml

 XDocument xdoc = XDocument.Load(Server.MapPath("path"));
 var val = xdoc.Descendants("Value").Where(i => i.Attribute("ID").Value == IDAttribute).FirstOrDefault().Attribute("Content").Value;

Inorder to use this you have to include System.Xml.Linq namespace

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.