在XML文件中找不到根元素 [英] Cannot find root element in XML file

查看:356
本文介绍了在XML文件中找不到根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建了一个XSD来为我的项目定义XML文件的结构。我使用XmlSpy创建了一个链接到XSD的XML文件。
问题是,如果我使用.NET XmlDocument读取文件然后查询根元素,那么

结果始终为null(1)。但是,如果我删除XmlSpy生成的所有

属性的根元素,那么使用.NET XML类(2)找到根

元素是没有问题的。


(1)查询根返回null的XML:

Hi all,

I created an XSD to define the structure of an XML file for my project. I
made an XML file linked to the XSD using XmlSpy. The problem is that if I
read the file using .NET XmlDocument and then query for the root element, the
result is always null (1). However if I strip the root element of all
attributes generated by XmlSpy, then there is no problem to find the root
element with .NET XML classes (2).

(1) The XML for which querying root returns null:

展开 | 选择 | Wrap | 行号

推荐答案




TT(Tom Tempelaere)写道:



TT (Tom Tempelaere) wrote:


< IncidentDefinitions xmlns = http://tempuri.org/XMLSchema.xsd"

xmlns:xsi =" http://www.w3.org/2001/ XMLSchema-instance"

xsi:schemaLocation =" http://tempuri.org/XMLSchema.xsd

C:/IncidentDefinitions.xsd">

....

< / IncidentDefinitions>
<IncidentDefinitions xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd
C:/IncidentDefinitions.xsd">
....
</IncidentDefinitions>


(3).NET中用于查询根元素的代码:

[code]

XmlDocument xmlDoc;

xmlDoc.Load(xmlFileName);

XmlNode node = xmlDoc.SelectSingleNode(&; IncidentDefinitions");
(3) The code in .NET to query for the root element:
[code]
XmlDocument xmlDoc;
xmlDoc.Load( xmlFileName );
XmlNode node = xmlDoc.SelectSingleNode( "IncidentDefinitions" );



如果您只想访问根元素那么

xmlDoc.DocumentElement

将执行任何操作元素的名称和

元素所具有的任何名称空间(XML样本中的名称空间是你的SelectSingleNode找不到元素的原因)。

因此,如果你需要根元素只需使用

XmlElement rootElement = xmlDoc.DocumentElement;

我认为使用XPath是不合理的和SelectSingleNode看起来像对象模型作为属性提供的



对于SelectSingleNode来查找某个带有XPath的元素

你需要的命名空间例如

XmlNamespaceManager namespaceManager = new

XmlNamespaceManager(xmlDoc.NameTable);

namespaceManager.AddNamespace(" pf"," http://tempuri.org/XMLSchema.xsd");

XmlElement rootElement =

xmlDoc.S electSingleNode(" pf:IncidentDefinitions",namespaceManager)as

XmlElement;


-


Martin Honnen --- MVP XML

http://JavaScript.FAQTs.com/

Well if you simply want to access the root element then
xmlDoc.DocumentElement
will do whatever the name of the element is and whatever namespace that
element has (where the namespace in your XML sample is the reason that
your SelectSingleNode does not find the element).
Thus if you need to root element simply use
XmlElement rootElement = xmlDoc.DocumentElement;
I don''t think it makes sense to use XPath and SelectSingleNode to look
for something the object model gives as a property.

As for SelectSingleNode to find an element with XPath in a certain
namespace you would need e.g.
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("pf", "http://tempuri.org/XMLSchema.xsd");
XmlElement rootElement =
xmlDoc.SelectSingleNode("pf:IncidentDefinitions", namespaceManager) as
XmlElement;


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


感谢Martin,这有帮助。 />

亲切的问候,

-

Tom Tempelaere。

Thanks Martin, that helped.

Kind regards,
--
Tom Tempelaere.


嗨Martin,


我可能有点过早地叫过Victory。没有你的替代

建议有效(我应该先测试)。请参阅内联部分评论。


" Martin Honnen"写道:
Hi Martin,

I may have cried Victory a little too early. None of your alternative
suggestions works (I should have tested first). See inline for some comment.

"Martin Honnen" wrote:

>

TT(Tom Tempelaere)写道:
>
TT (Tom Tempelaere) wrote:


< IncidentDefinitions xmlns =" http://tempuri.org/XMLSchema.xsd"

xmlns:xsi =" http://www.w3.org/2001/ XMLSchema-instance"

xsi:schemaLocation =" http://tempuri.org/XMLSchema.xsd

C:/IncidentDefinitions.xsd">

....

< / IncidentDefinitions>

<IncidentDefinitions xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/XMLSchema.xsd
C:/IncidentDefinitions.xsd">
....
</IncidentDefinitions>


(3).NET中用于查询根元素的代码:

[code]

XmlDocument xmlDoc;

xmlDoc.Load(xmlFileName);

XmlNode node = xmlDoc.SelectSingleNode(&; IncidentDefinitions");
(3) The code in .NET to query for the root element:
[code]
XmlDocument xmlDoc;
xmlDoc.Load( xmlFileName );
XmlNode node = xmlDoc.SelectSingleNode( "IncidentDefinitions" );



如果您只想访问根元素那么

xmlDoc.DocumentElement

将执行任何操作元素的名称和

元素所具有的任何名称空间(XML样本中的名称空间是你的SelectSingleNode找不到元素的原因)。

因此,如果你需要root元素,只需使用

XmlElement rootElement = xmlDoc.DocumentElement;


Well if you simply want to access the root element then
xmlDoc.DocumentElement
will do whatever the name of the element is and whatever namespace that
element has (where the namespace in your XML sample is the reason that
your SelectSingleNode does not find the element).
Thus if you need to root element simply use
XmlElement rootElement = xmlDoc.DocumentElement;



说实话,我认为这样可以解决问题,但实际上它不会是b $ b。如果不是查询''IncidentDefinitions''根元素我使用

xmlDocument.DocumentElement我还有一个问题:我找不到任何其他的

节点在文档中。

Well to be honest I thought this would solve the problem, but it actually
doesn''t. If instead of querying the ''IncidentDefinitions'' root element I use
xmlDocument.DocumentElement I still have a problem: I can''t find any other
nodes that are in the document.


我不认为使用XPath和SelectSingleNode来查看

对象模型给出的内容作为一个属性。


至于SelectSingleNode在某个

命名空间中找到一个带有XPath的元素,你需要例如

XmlNamespaceManager namespaceManager = new

XmlNamespaceManager(xmlDoc.NameTable);

namespaceManager.AddNamespace(" pf"," http://tempuri.org/XMLSchema.xsd") ;

XmlElement rootElement =

xmlDoc.SelectSingleNode(" pf:IncidentDefinitions",namespaceManager)as

XmlElement;
I don''t think it makes sense to use XPath and SelectSingleNode to look
for something the object model gives as a property.

As for SelectSingleNode to find an element with XPath in a certain
namespace you would need e.g.
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(xmlDoc.NameTable);
namespaceManager.AddNamespace("pf", "http://tempuri.org/XMLSchema.xsd");
XmlElement rootElement =
xmlDoc.SelectSingleNode("pf:IncidentDefinitions", namespaceManager) as
XmlElement;



也不起作用。它确实返回一个元素,但查询任何

文档节点失败。


看起来好像XmlSpy添加的属性不能

由.NET XML框架正确解析,或至少使其混淆。或者也许

还有别的东西我说的不对吗?


同时我要问我的客户不要使用根元素的属性发布任何XML文件


Doesn''t work either. It does return an element, but querying for any of the
documents nodes fails.

It appears as though the attributes that are added by XmlSpy cannot be
parsed correctly by .NET XML framework, or at least confuses it. Or maybe
there is something else still that I''m not doing correct?

In the meanwhile I''ll have to ask my clients to not publish any XML files
with attributes for the root element.


-

Martin Honnen --- MVP XML

http://JavaScript.FAQTs.com/
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/



亲切问候,

-

Tom Tempelaere。

Kind regards,
--
Tom Tempelaere.


这篇关于在XML文件中找不到根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆