XmlDocument.LoadXml和命名空间 [英] XmlDocument.LoadXml and namespaces

查看:75
本文介绍了XmlDocument.LoadXml和命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;


一旦我将一个xml文件加载到一个DOM中,有没有办法获得任何

名称空间声明xml文件?或者那些刚被丢弃的人随着DOM的填充而被丢弃了吗?


如果你不能从DOM中获取它,那么任何其他方式来获取它命名空间?

我假设做了一个SAX读取,返回包括NameSpaces

在内的每个元素并将其拉出来(并在同一时间构建DOM)时间)?


在我的情况下,我得到一个流,所以我不能读两次,因为流可能不会支持重置。它可能有NS元素 - 但我不知道是否有任何或者它们是什么或者它们是什么,除非通过阅读流。


-

谢谢 - 戴夫

Hi;

Once I have loaded an xml file into a DOM, is there any way to then get any
namespace declarations that were in the xml file? Or are those just discarded
as the DOM is populated?

And if you can''t get it from the DOM, any other way to get the namespace(s)?
I''m assuming doing a SAX read which returns each element including NameSpaces
and pull it form that (and build up the DOM at the same time)?

In my case I get a Stream so I can''t read it twice as the Stream may not
support reset. And it may have NS elements - but I don''t know if there are
any or what they are except by reading the Stream.

--
thanks - dave

推荐答案

David Thielen写道:
David Thielen wrote:
有一次我已经将一个xml文件加载到DOM中,有没有办法获得xml文件中的任何名称空间声明?或者那些刚被丢弃的人随着DOM的填充而被丢弃?
Once I have loaded an xml file into a DOM, is there any way to then get any
namespace declarations that were in the xml file? Or are those just discarded
as the DOM is populated?




当然不是。您可以使用

doc.SelectNodes(" // namespace :: *")选择名称空间声明。


-

Oleg Tkachenko [XML MVP,MCAD]
http://www.xmllab.net
http://blog.tkachenko.com


嗨;


这种方式有效。我在文件的标题中声明了3个名称空间。

SelectNodes(" // namespace :: *")给了我36个节点。我没有检查每一个

但它似乎是一遍又一遍地重复的3个命名空间。


任何想法为什么我没有得到只是3 ?


-

谢谢 - 戴夫

" Oleg Tkachenko [MVP]"写道:
Hi;

This sort-of worked. I have 3 namespaces declared in the header of the file.
The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
but it appeared to be the 3 namespaces repeated over and over.

Any idea why I didn''t get just 3?

--
thanks - dave
"Oleg Tkachenko [MVP]" wrote:
David Thielen写道:
David Thielen wrote:
一旦我将一个xml文件加载到DOM中,有什么方法可以获得任何
xml文件中的命名空间声明?或者那些刚被丢弃的人随着DOM的填充而被丢弃?
Once I have loaded an xml file into a DOM, is there any way to then get any
namespace declarations that were in the xml file? Or are those just discarded
as the DOM is populated?



当然不是。您可以使用
doc.SelectNodes(" // namespace :: *")选择名称空间声明。

-
Oleg Tkachenko [XML MVP,MCAD]
http://www.xmllab.net
http://blog.tkachenko.com



对于xml:

< root xmlns =" http://www.test.org" xmlns:sns =" http://www.test.org/sub"

xmlns:mns =" http://www.test.org/mini">

< data>

< items>

< item id =" 1">

< sns :subItem sid =" 11"> dave< / sns:subItem>

< sns:subItem sid =" 12"> thielen< / sns:subItem>

< / item>

< item id =" 2">

< sns:subItem sid =" 21"> shirley< / sns:subItem>

< / item>

< / items>

< mns:more> dave thielen< / mns :更多>

< / data>

< / root>


返回的节点是:

mns = http://www.test.org/mini

sns = http://www.test.org/sub

{default} = http://www.test.org

xml = http://www.w3.org/XML/1998/namespace

mns = http:// www。 test.org/mini

sns = http://www.test.org/sub

{默认} = http://www.test.org

xml = h ttp://www.w3.org/XML/1998/namespace

mns = http://www.test.org/mini

sns = http:// www.test.org/sub

{默认} = http://www.test.org

xml = http://www.w3.org/XML/ 1998 /名称空间

mns = http://www.test.org/mini

sns = http://www.test.org/sub

{默认} = http://www.test.org

xml = http://www.w3.org/XML/1998/namespace

mns = http://www.test.org/mini

sns = http://www.test.org/sub

{default} = http:// www .test.org

xml = http://www.w3.org/XML/1998/namespace

mns = http://www.test.org/mini

sns = http://www.test.org/sub

{默认} = http://www.test.org

xml = http://www.w3.org/XML/1998/namespace

mns = http://www.test.org/mini

sns = http: //www.test.org/sub

{默认} = http://www.test.org

xml = http://www.w3.org/ XML / 1998 / namespace

mns = http://www.test.org/mini

sns = http://www.test.org/sub

{默认} = http ://www.test.org

xml = http://www.w3.org/XML/1998/namespace

mns = http://www.test .org / mini

sns = http://www.test.org/sub

{默认} = http://www.test.org

xml = http://www.w3.org/XML/1998/namespace


??? - 谢谢 - 戴夫
For the xml:
<root xmlns="http://www.test.org" xmlns:sns="http://www.test.org/sub"
xmlns:mns="http://www.test.org/mini">
<data>
<items>
<item id="1">
<sns:subItem sid="11">dave</sns:subItem>
<sns:subItem sid="12">thielen</sns:subItem>
</item>
<item id="2">
<sns:subItem sid="21">shirley</sns:subItem>
</item>
</items>
<mns:more>dave thielen</mns:more>
</data>
</root>

The nodes returned are:
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace
mns=http://www.test.org/mini
sns=http://www.test.org/sub
{default}=http://www.test.org
xml=http://www.w3.org/XML/1998/namespace

??? - thanks - dave


这篇关于XmlDocument.LoadXml和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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