从混合XML和纯文本的流读出文本、XML

2023-09-07 09:07:09 作者:冋忆妸洧妸嘸

我有一个包含两个任意纯文本和格式良好的XML元素的部分文本流。我怎样才能阅读并仅提取XML元素?的XmlReader与ConformanceLevel设置为片段仍然抛出一个异常,遇到纯文本,其中有畸形XML时。

任何想法?谢谢

下面是我的code到目前为止:

  XmlReaderSettings设置=新XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

使用(XmlReader的读者= XmlReader.Create(流设置))
    而(!reader.EOF)
    {
        reader.MoveToContent();
        XmlDocument的文档=新的XmlDocument();
        doc.Load(reader.ReadSubtree());
        reader.ReadEndElement();
    }
 

下面是一个示例流内容,我有没有对其进行控制的方式:

发现了两个对象: 对象 <对象>     <名称> A< /名称>     <描述>< /描述> &所述; /对象> 对象B <对象>     <名称> B< /名称>     <描述>< /描述> &所述; /对象> 如何使用dom4j解析xml来获取文本的内容

解决方案

只要这是一个黑客,如果你有一个假的XML根节点包装你的混合文档,你应该能够做什么,你只需要得到型元件(即跳过文本节点)之间的根元素的子节点:

 使用系统;
使用System.Linq的;
使用的System.Xml;

静态类节目{

    静态无效的主要(字串[] args){

        串混= @
发现了两个对象:
对象
<对象>
    <名称> A< /名称>
    <描述>< /描述>
&所述; /对象>
对象B
<对象>
    <名称> B< /名称>
    <描述>< /描述>
&所述; /对象>
;
        XML字符串=< FOO>中+混合+< / FOO>中;
        XmlDocument的文档=新的XmlDocument();
        doc.LoadXml(XML);
        VAR xmlFragments从doc.FirstChild.ChildNodes XmlNode的节点=
                           其中,node.NodeType == XmlNodeType.Element
                           选择节点;
        的foreach(在xmlFragments VAR片段){
            Console.WriteLine(fragment.OuterXml);
        }

    }

}
 

I have a text stream that contains segments of both arbitrary plain text and well-formed xml elements. How can I read it and extract the xml elements only? XmlReader with ConformanceLevel set to Fragment still throws an exception when it encounters plain text, which to it is malformed xml.

Any ideas? Thanks

Here's my code so far:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

using (XmlReader reader = XmlReader.Create(stream, settings))
    while (!reader.EOF)
    {
        reader.MoveToContent();
        XmlDocument doc = new XmlDocument();
        doc.Load(reader.ReadSubtree());
        reader.ReadEndElement();
    }

Here's a sample stream content and I have no control over it by the way:

Found two objects:
Object a
<object>
    <name>a</name>
    <description></description>
</object>
Object b
<object>
    <name>b</name>
    <description></description>
</object>

解决方案

Provided that this is a hack, if you wrap your mixed document with a "fake" xml root node, you should be able to do what you need getting only the nodes of type element (i.e. skipping the text nodes) among the children of the root element:

using System;
using System.Linq;
using System.Xml;

static class Program {

    static void Main(string[] args) {

        string mixed = @"
Found two objects:
Object a
<object>
    <name>a</name>
    <description></description>
</object>
Object b
<object>
    <name>b</name>
    <description></description>
</object>
";
        string xml = "<FOO>" + mixed + "</FOO>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        var xmlFragments = from XmlNode node in doc.FirstChild.ChildNodes 
                           where node.NodeType == XmlNodeType.Element 
                           select node;
        foreach (var fragment in xmlFragments) {
            Console.WriteLine(fragment.OuterXml);
        }

    }

}

 
精彩推荐
图片推荐