我无法使用C#读取xml文件中的值

时间:2019-07-24 15:13:14

标签: c# xml

我有一个具有以下结构的xml文件,并且在读取文本Var_A ecc之前无法读取``Var_''值。 我在做什么错了?

<VarList>
<VarProject Var_A="1" Var_B="1" Var_C="" Var_D="" VarE="">text_text_text</VarProject>
</VarList>

这是我的代码:

using System;
using System.Xml;

namespace ReadXMLfromFile
{
    class Class1
    {
        static void Main(string[] args)
        {
            XmlTextReader reader = new XmlTextReader("file.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我认为,您错过了阅读属性的过程。 您可以按照描述here的方式进行操作:

    XmlTextReader reader = new XmlTextReader("file.xml");
    while (reader.Read())
    {
        reader.NodeType.Dump();
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                Console.Write("<" + reader.Name);
                Console.WriteLine(">");
                  while (reader.MoveToNextAttribute()) {
                    Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
                  } 
                  break;
            case XmlNodeType.Text:
                Console.WriteLine(reader.Value);
                break;
            case XmlNodeType.EndElement:
                Console.Write("</" + reader.Name);
                Console.WriteLine(">");
                break;
        }
    }

答案 1 :(得分:0)

您仅读取Node Element部分,而不是childNodes 要读取子节点,您可以使用此代码

        XmlTextReader reader = new XmlTextReader(@"youfilePath");
        XmlDocument doc = new XmlDocument();
        XmlNode node = doc.ReadNode(reader);
        foreach (XmlNode item in node.ChildNodes)
        {
            // node stuff...
            if (item.Attributes != null)
            {
                foreach (XmlAttribute att in item.Attributes)
                {
                    // attribute stuff
                }
            }
        }

所以要做这样的事情

 while (reader.Read())
        {


            switch (reader.NodeType)
            { 
                case XmlNodeType.Element:
                    Console.Write(" < " + reader.Name);
                    Console.WriteLine(">");
                    if (reader.HasAttributes)
                    {
                        while (reader.MoveToNextAttribute())
                        {
                            Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
                        }
                    }
                    break;
                case XmlNodeType.Text:
                    Console.WriteLine(reader.Value);
                    break;

                case XmlNodeType.EndElement:
                    Console.Write("</" + reader.Name);
                    Console.WriteLine(">");
                    break;
            }
        }

结果应该是:

enter image description here

答案 2 :(得分:0)

您可以使用LINQ XDocument

 XDocument xml = XDocument.Load("file.xml");

            foreach(XElement element in xml.Descendants())
            {
                // in this example there is only one xElement but that element contains all of our attributes
                // we could also display the element value here (example: text_text_text)
                IEnumerable<XAttribute> attributes = element.Attributes();

                foreach(XAttribute attribute in attributes)
                {
                    // display the attribute name (example: "var_A")
                    Console.WriteLine(attribute.Name);
                    // display the attribute value (example: "1")
                    Console.WriteLine(attribute.Value);
                }
            }