1

I want select all values from node but something is wrong because message box don't show anything. XML file is in this same folder where is project.

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<names>
    <file>
        <name>Test name 1</name> 
        <author>Test author 1</author> 
        <version>1.0</version> 
    </file>

    <file>
        <name>Test name 2</name> 
        <author>Test author 2</author> 
        <version>2.0</version> 
    </file>

    <file>
        <name>Test name 3</name> 
        <author>Test author 3</author> 
        <version>3.0</version> 
    </file>

</names>

C# Code:

XmlDocument xml = new XmlDocument();
xml.LoadXml(Files.xml); 

XmlNodeList xnList = xml.SelectNodes("names/file/name");
foreach (XmlNode xn in xnList)
{
    MessageBox.Show(xn.ToString());
}
1
  • I remember faintly that the path should be rooted with a leading slash, /names/file/name Commented Feb 12, 2018 at 13:36

3 Answers 3

2

LoadXml loads the XML document from the specified string. If you want to load the xml by path, use Load(filePath).

 XmlDocument xml = new XmlDocument();
 xml.Load(@"C:\Sample.xml");

 XmlNodeList xnList = xml.SelectNodes("names/file/name");
 foreach (XmlNode xn in xnList)
 {
     Console.WriteLine(xn.InnerText);
 }

// outputs,
// Test name 1
// Test name 2
// Test name 3
Sign up to request clarification or add additional context in comments.

Comments

0

You should verify the Files.xml property is a string that contains the example XML (assuming it is a property). LoadXml loads the XML data directly from a given string.

I have used your code as it is and it works properly. It would be better to use a // prefix in the SelectNodes method call to start at the root, but even without this change it should work as expected.

XmlDocument xml = new XmlDocument();
xml.LoadXml(File.ReadAllText("test.xml"));

XmlNodeList xnList = xml.SelectNodes("//names/file/name");
foreach (XmlNode xn in xnList)
{
    Console.WriteLine(xn.ToString());
}

Comments

0

It would be more easier for you if you used values "attributes" instead of adding nodes and changing the inner text into it, and it's going to be easier to set, and get. For Example if your XML Like this

<names>
    <file name="Test name 1" author="Test author 1" version="1.0" />
    <file name="Test name 2" author="Test author 2" version="2.0" />
    <file name="Test name 3" author="Test author 3" version="3.0" />
</names>

For you Code you can use the following

    try
                    {
                        XDocument doc = XDocument.Load(@"C:\Sample.xml");
                        foreach (var files in doc.Descendants("names"))
                        {
//remember value will be null if the attribute is missing
//to present the values it is going to be 
 Console.WriteLine("File Name : " + (string)files.Attribute("name") + ", File Author :" + (string)files.Attribute("author") + ", File Version : " + (string)files.Attribute("version"));
//if you want to set a specific attribute                           
                            if ((string)files.Attribute("name") == "Example")
                            {
                                task.SetAttributeValue("author", "Example Author");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                       //your exception here
                    }

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.