简体   繁体   English

C#XML从节点选择值

[英]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: 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: C#代码:

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. LoadXml从指定的字符串加载XML文档。 If you want to load the xml by path, use Load (filePath). 如果要按路径加载xml,请使用加载 (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). 您应该验证Files.xml属性是包含示例XML的string (假定它是一个属性)。 LoadXml loads the XML data directly from a given string . LoadXml直接从给定的string加载XML数据。

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. 最好在SelectNodes方法调用中使用//前缀以从根开始,但是即使没有此更改,它也应能按预期工作。

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 例如,如果您的XML像这样

<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
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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