本文介绍了XML错误:有多个根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个Web服务越来越XML。下面是XML的样子:

I am getting XML from a web service. Here is what the XML looks like:

<parent>
    <child>
        Text
    </child>
</parent>
<parent>
    <child>
        <grandchild>
            Text
        </grandchild>
        <grandchild>
            Text
        </grandchild>
    </child>
    <child>
        Text
    </child>
</parent>

etc.

这是我的C#code:

And here is my C# code:

StringBuilder output = new StringBuilder();

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xoResponse.@return)))
{
    XmlWriterSettings ws = new XmlWriterSettings();
    //ws.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(output, ws))
    {
        // Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Name);
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
            }
        }
    }
}

我认为错误是第二父元素抛出。我怎样才能避免这个错误?任何帮助是极大的AP preciated。

I believe that the error is thrown on the second parent element. How can I avoid this error? Any help is greatly appreciated.

推荐答案

您需要附上&LT;父&gt;在周围的元素元素作为XML文档只能有一个根节点:

You need to enclose your <parent> elements in a surrounding element as XML Documents can have only one root node:

<parents> <!-- I've added this tag -->
    <parent>
        <child>
            Text
        </child>
    </parent>
    <parent>
        <child>
            <grandchild>
                Text
            </grandchild>
            <grandchild>
                Text
            </grandchild>
        </child>
        <child>
            Text
        </child>
    </parent>
<parents> <!-- I've added this tag -->

当你从别的地方获得这个标记,而不是生成它自己,你可能需要通过治疗响应作为一个字符串,并用适当的标签包装它,尝试分析它作为XML之前,该自己做。

As you're receiving this markup from somewhere else, rather than generating it yourself, you may have to do this yourself by treating the response as a string and wrapping it with appropriate tags, prior to attempting to parse it as XML.

所以,你已经有几条路:

So, you've a couple of choices:


  1. 获取Web服务的提供者返回你的实际的XML有一个根节点

  2. pre-过程中的XML,正如我上面所说,添加一个根节点

  3. pre-过程中的XML将其分割成多个数据块(即一个用于每个&LT;父&GT; 节点)和流程中的每个作为一个独特的XML文档

  1. Get the provider of the web service to return you actual XML that has one root node
  2. Pre-process the XML, as I've suggested above, to add a root node
  3. Pre-process the XML to split it into multiple chunks (i.e. one for each <parent> node) and process each as a distinct XML Document

这篇关于XML错误:有多个根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:14