【问题标题】:XML parsing in C#. Issue with DescendantsC# 中的 XML 解析。后代问题
【发布时间】:2016-01-14 23:58:57
【问题描述】:

我有一个需要解析的 XML 字符串。

代码如下:

inputXML = "<elementList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
            <NodePrime>
                <Node>
                     <NodeA>1</NodeA>
                     <NodeB>2</NodeB>
                </Node>
                <Node>
                     <NodeA>3</NodeA>
                     <NodeB>4</NodeB>
                </Node>
             </NodePrime>
             </elementList>";

var ItemList = new List<ItemList>();
using(XmlReader Reader = XmlReader.Create(new StringReader(inputXML)))
{
     Reader.read();
     var doc = XDocument.Load(Reader);
     var xmlnode = doc.Descendants("Node");
     foreach(var item in xmlHeirarchy)
     {
         var ListA = new ItemList
         {
             itemA = item.Element("NodeA").Value;
             itemB = item.Element("NodeB").Value;
         };
         ItemList.Add(ListA );
     }
}

我的问题在于 doc.Descendants,因为我的 xmlnode 返回空。

请让我知道我做错了什么以及最好的解决方法。

【问题讨论】:

  • xmlHeirarchy -> xmlnode.这是什么Reader.read();
  • 除了一些拼写错误之外,代码运行良好。你确定这是你的真实代码吗? inputXML 是硬编码的吗?
  • 尝试类似 doc.Root.Element("NodePrime").Elements().Descendants("Node")

标签: c# xml xml-parsing xmlreader


【解决方案1】:

您可以使用XDocument.Parse() 从字符串中填充XDocument,这比您当前的方法更简单。然后doc.Descendants("Node") 应该如下所示:

var inputXML = @"<elementList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
    <NodePrime>
        <Node>
             <NodeA>1</NodeA>
             <NodeB>2</NodeB>
        </Node>
        <Node>
             <NodeA>3</NodeA>
             <NodeB>4</NodeB>
        </Node>
     </NodePrime>
     </elementList>";

var ItemList = new List<ItemList>();
var doc = XDocument.Parse(inputXML);
var xmlnode = doc.Descendants("Node");
foreach(var item in xmlnode)
{
    var ListA = new ItemList
    {
        itemA = item.Element("NodeA").Value,
        itemB = item.Element("NodeB").Value
    };
    ItemList.Add(ListA );
}
Console.WriteLine(ItemList.Count());

dotnetfiddle demo

输出:

2

【讨论】:

    【解决方案2】:

    如果你有一个大文件,我喜欢这样做。使用 XmlTextReader 比 XmlReader 使用更少的内存。唯一的区别是 XmlReader 允许在 Xml 中向后移动,而 XmlTextReader 仅用于转发

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputXML = "<elementList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
                                "<NodePrime>" +
                                    "<Node>" +
                                         "<NodeA>1</NodeA>" +
                                         "<NodeB>2</NodeB>" +
                                    "</Node>" +
                                    "<Node>" +
                                         "<NodeA>3</NodeA>" +
                                         "<NodeB>4</NodeB>" +
                                    "</Node>" +
                                 "</NodePrime>" +
                            "</elementList>";
                StringReader sReader = new StringReader(inputXML);
                XmlTextReader reader = new XmlTextReader(sReader);
                List<ItemList> itemList = new List<ItemList>();
                while (!reader.EOF)
                {
                    if (reader.Name != "Node")
                    {
                        reader.ReadToFollowing("Node");
                    }
                    if(!reader.EOF)
                    {
                        XElement node = (XElement)XElement.ReadFrom(reader);
                        itemList.Add(new ItemList() {
                           itemA = (int)node.Element("NodeA"),
                           itemB = (int)node.Element("NodeB")
                        });
                    }
                }
    
            }
        }
        public class ItemList
        {
            public int itemA { get; set; }
            public int itemB { get; set; }
        }
    }
    

    【讨论】:

    • XmlTextReader is 一个 XmlReader 并且都允许只进的访问。从 .NET 2.0 开始,Microsoft guidance不能使用XmlTextReader
    • 感谢您的回复。我看到主要问题是我的 XML 字符串。它有很多垃圾字符。我想我需要想办法清理我的 XML 字符串。
    • 我将您的字符串分成几行以减少不可打印字符的数量。
    猜你喜欢
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多