简体   繁体   English

使用xDocument或XmlDocument C#读取无效的xml

[英]Reading invalid xml with xDocument or XmlDocument c#

I have situation where I have used xslt to transform xml file. 我有使用xslt转换xml文件的情况。

Now I need to modify the result xml file which is not valid xml and xml parsers are not able to read it. 现在,我需要修改无效的xml结果文件,并且xml解析器无法读取它。

It doesn't start with xml declaration and there is no one root for the file. 它不是以xml声明开始的,并且该文件没有一个根。

I cannot change the structure of the file as that is another standard that I need to use but I need to add node inside the valid xml and also get information from specific node. 我无法更改文件的结构,因为这是我需要使用的另一个标准,但是我需要在有效xml内添加节点,并从特定节点获取信息。

I already tried to use Like this 我已经尝试使用像这样

XmlDocument doc = new XmlDocument();
doc.XmlResolver = null;
doc.Load(InputFile);
doc.DocumentElement;

With this I only got stuff from inside the invalid XML but not from inside the valid XML 这样,我只能从无效的XML内部获取内容,而不能从有效的XML内部获取内容

What I would really need is list of "validXmlWithDeclaration" nodes 我真正需要的是“ validXmlWithDeclaration”节点列表

structure is something like this. 结构就是这样。

<invalidXMLWithoutDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</invalidXMLWithoutDeclaration>
<validXmlWithDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</validXmlWithDeclaration>
<invalidXMLWithoutDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</invalidXMLWithoutDeclaration>
<validXmlWithDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</validXmlWithDeclaration>
<invalidXMLWithoutDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</invalidXMLWithoutDeclaration>
<validXmlWithDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</validXmlWithDeclaration>

Here is an example that parses the snippet you have shown by settings the InnerXml property of an XmlDocumentFragment and selects some of the elements in it: 这是一个示例,它通过设置XmlDocumentFragmentInnerXml属性来解析您显示的代码片段,并选择其中的一些元素:

        XmlDocument doc = new XmlDocument();
        XmlDocumentFragment fragment = doc.CreateDocumentFragment();
        fragment.InnerXml = @"<invalidXMLWithoutDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</invalidXMLWithoutDeclaration>
<validXmlWithDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</validXmlWithDeclaration>
<invalidXMLWithoutDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</invalidXMLWithoutDeclaration>
<validXmlWithDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</validXmlWithDeclaration>
<invalidXMLWithoutDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</invalidXMLWithoutDeclaration>
<validXmlWithDeclaration>
 <foo>
  <bar>
  </bar>
 </foo>
</validXmlWithDeclaration>";
        foreach (XmlElement el in fragment.SelectNodes("validXmlWithDeclaration"))
        {
            Console.WriteLine(el.OuterXml);
        }

You just don't have a well formed xml file. 您只是没有格式正确的xml文件。 See my solution below with XmlRead and XDocument 请参阅下面的XmlRead和XDocument解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication62
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlReader reader = XmlReader.Create(FILENAME);

            while (!reader.EOF)
            {
                if (reader.Name != "invalidXMLWithoutDeclaration")
                {
                    reader.ReadToFollowing("invalidXMLWithoutDeclaration");
                }
                if (!reader.EOF)
                {
                    XElement invalidXMLWithoutDeclaration = (XElement)XElement.ReadFrom(reader);
                }
            }

        }

    }
}

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

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