C# 即使XML格式良好,我也会得到一个空的XmlNodeList
我有一个格式良好的C# 即使XML格式良好,我也会得到一个空的XmlNodeList,c#,.net,xml,C#,.net,Xml,我有一个格式良好的xml,我想解析和检索行元素。 但我一直得到一个空的XmlNodeList块。我做错了什么? 谢谢 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/20
xml
,我想解析和检索行
元素。
但我一直得到一个空的XmlNodeList
块。我做错了什么?
谢谢
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>
信息隐私
你能读懂这个吗
这不可能发生吗?
试试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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序16
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XNamespace ns=((XElement)doc.FirstNode).Name.Namespace;
Document.documents=doc.substands(ns+“par”)。选择(x=>newdocument(){
lineSpacing=(int?)x.Attribute(“lineSpacing”),
lines=x.Elements(ns+“line”)。选择(y=>newline(){
基线=(int)y.Attribute(“基线”),
l=(int)y.属性(“l”),
t=(int)y.属性(“t”),
r=(int)y.属性(“r”),
b=(int)y.属性(“b”),
格式化=(字符串)y.Element(ns+“格式化”),
lang=(字符串)y.Element(ns+“格式化”).Attribute(“lang”)
})托利斯先生()
}).ToList();
}
}
公共类文档
{
公共静态列表文档=新列表();
公共整数?行距{get;set;}
公共列表行{get;set;}
}
公共班级线
{
公共int基线{get;set;}
公共整数l{get;set;}
公共int t{get;set;}
公共int r{get;set;}
公共int b{get;set;}
公共字符串格式{get;set;}
公共字符串lang{get;set;}
}
}
试试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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序16
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XNamespace ns=((XElement)doc.FirstNode).Name.Namespace;
Document.documents=doc.substands(ns+“par”)。选择(x=>newdocument(){
lineSpacing=(int?)x.Attribute(“lineSpacing”),
lines=x.Elements(ns+“line”)。选择(y=>newline(){
基线=(int)y.Attribute(“基线”),
l=(int)y.属性(“l”),
t=(int)y.属性(“t”),
r=(int)y.属性(“r”),
b=(int)y.属性(“b”),
格式化=(字符串)y.Element(ns+“格式化”),
lang=(字符串)y.Element(ns+“格式化”).Attribute(“lang”)
})托利斯先生()
}).ToList();
}
}
公共类文档
{
公共静态列表文档=新列表();
公共整数?行距{get;set;}
公共列表行{get;set;}
}
公共班级线
{
公共int基线{get;set;}
公共整数l{get;set;}
公共int t{get;set;}
公共int r{get;set;}
公共int b{get;set;}
公共字符串格式{get;set;}
公共字符串lang{get;set;}
}
}
这是因为
元素有一个名称空间
试试这个:
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);
或者,如果可能,从文档中删除名称空间并使用原始代码。这是因为
元素有名称空间
试试这个:
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);
或者,如果可能,从文档中删除名称空间并使用原始代码。注意http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml
不存在。请注意http://www.abbyy.com/FineReader_xml/FineReader10-schema-v1.xml
不存在。这正是我想要的。但是,我不熟悉var
声明的隐式类型。什么是pars
?类型为…?的集合,它包含行
,这是一个包含5个int
和3个string
值的结构集合。我说得对吗?如果我想让你使用我的oown类进行lines集合,我将如何进行?假设我有类文本行{public string lang;public string formatting;public int baseline;public int left;public int top;public int right;public int bottom;}
它们是xml文件中的标记(元素)。我使用Linq方法创建匿名类型。我更新了代码以使用匿名类型或类结构。这正是我想要的。但是,我不熟悉var
声明的隐式类型。什么是pars
?类型为…?的集合,它包含行
,这是一个包含5个int
和3个string
值的结构集合。我说得对吗?如果我想让你使用我的oown类进行lines集合,我将如何进行?假设我有类文本行{public string lang;public string格式化;public int基线;public int left;public int to