虽然XML格式正确,但我得到一个空的XmlNodeList事件

时间:2016-10-06 15:43:52

标签: c# .net xml

我有一个格式良好的xml,我想解析并检索line元素。 但我一直空洞XmlNodeList blocks。我究竟做错了什么? 谢谢!

XmlDocument doc = new XmlDocument();
doc.Load("file.xml"));
XmlNodeList blocks = doc.DocumentElement.SelectNodes("/document/page/block");

foreach (XmlNode block in blocks)
{

  //keep iterating over `par` elements in `text`
}

file.xml:

 <document xmlns="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml" version="1.0" producer="ABBYY FineReader Engine 11" languages="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml">
    <page width="1043" height="653" resolution="300" originalCoords="1">
    <block blockType="Text" blockName="" l="43" t="27" r="736" b="147"><region><rect l="641" t="27" r="735" b="28"/><rect l="520" t="28" r="735" b="29"/><rect l="399" t="29" r="735" b="30"/><rect l="277" t="30" r="735" b="31"/><rect l="156" t="31" r="735" b="32"/><rect l="43" t="32" r="735" b="83"/><rect l="43" t="83" r="736" b="86"/><rect l="44" t="86" r="736" b="142"/><rect l="44" t="142" r="643" b="143"/><rect l="44" t="143" r="521" b="144"/><rect l="44" t="144" r="400" b="145"/><rect l="44" t="145" r="279" b="146"/><rect l="44" t="146" r="157" b="147"/></region>
        <region>

        </region>

        <text>
            <par lineSpacing="816">
                <line baseline="58" l="314" t="28" r="734" b="55">
                    <formatting lang="EnglishUnitedStates">Information priveid</formatting>
                </line>
                <line baseline="92" l="377" t="61" r="673" b="89">
                    <formatting lang="EnglishUnitedStates">Canyouread this</formatting>
                </line>
            </par>
            <par>
                <line baseline="146" l="45" t="110" r="679" b="146">
                <formatting lang="EnglishUnitedStates"> This can not be happening?</formatting>
                </line>
            </par>
        </text>
    </block>
</page>
</document> 

2 个答案:

答案 0 :(得分:2)

这是因为您的<document>元素具有命名空间。

试试这个:

var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("nsp", "http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml");

XmlNodeList blocks = doc.SelectNodes("//nsp:page/nsp:block", nsManager);

或者,如果可能,请从文档中删除命名空间并使用原始代码。

答案 1 :(得分:0)

试试xml linq

以下是使用匿名类型

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

namespace ConsoleApplication16
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = ((XElement)doc.FirstNode).Name.Namespace;
            var pars = doc.Descendants(ns + "par").Select(x => new {
                lineSpacing = (int?)x.Attribute("lineSpacing"),
                lines = x.Elements(ns + "line").Select(y => new {
                    baseline= (int)y.Attribute("baseline"),
                    l = (int)y.Attribute("l"),
                    t = (int)y.Attribute("t"),
                    r = (int)y.Attribute("r"),
                    b = (int)y.Attribute("b"),
                    formatting = (string)y.Element(ns + "formatting"),
                    lang = (string)y.Element(ns + "formatting").Attribute("lang")
                }).ToList()
            }).ToList();

        }

    }

}

下面是使用类结构

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

namespace ConsoleApplication16
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace ns = ((XElement)doc.FirstNode).Name.Namespace;
            Document.documents  = doc.Descendants(ns + "par").Select(x => new Document() {
                lineSpacing = (int?)x.Attribute("lineSpacing"),
                lines = x.Elements(ns + "line").Select(y => new Line() {
                    baseline = (int)y.Attribute("baseline"),
                    l = (int)y.Attribute("l"),
                    t = (int)y.Attribute("t"),
                    r = (int)y.Attribute("r"),
                    b = (int)y.Attribute("b"),
                    formatting = (string)y.Element(ns + "formatting"),
                    lang = (string)y.Element(ns + "formatting").Attribute("lang")
                }).ToList()
            }).ToList();

        }

    }
    public class Document
    {
        public static List<Document> documents = new List<Document>();
        public int? lineSpacing { get; set; }
        public List<Line> lines { get; set; }
    }
    public class Line
    {
        public int baseline { get; set; }
        public int l { get; set; }
        public int t { get; set; }
        public int r { get; set; }
        public int b { get; set; }
        public string formatting { get; set; }
        public string lang { get; set; }
    }
}