简体   繁体   中英

C# XML select value from node

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

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

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

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
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM