如何使用XPathNavigator插入CDATA部分? [英] How to insert a CDATA section using XPathNavigator ?

查看:63
本文介绍了如何使用XPathNavigator插入CDATA部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我如何使用XPathNavigator插入CDATA部分吗?我已经尝试过没有运气了:


XmlDocument docNav = new XmlDocument();

docNav.LoadXml(xmlString );

XPathNavigator nav = docNav.CreateNavigator();


XmlDocument doc = new XmlDocument();

doc.LoadXml ("< DocumentData>< / DocumentData>");

XmlElement elem = doc.CreateElement(currentNodeName);

elem.AppendChild(doc.CreateCDataSection( dataString));

doc.DocumentElement.AppendChild(elem);

XPathNavigator newNav = doc.CreateNavigator();


nav.InsertAfter(newNav);

问候,

Eric.-

解决方案

嗨Eric,


当您通过XPathNavigator界面修改XmlDocument时,您需要注册

以使用XPath数据模型,该模型略有不同

XmlDocument。其中一个不同之处就是缺少CDATA部分,这些部分与任何其他纯文本一样对待。请注意,XPathNodeType枚举

没有CDATA部分的相应字段,而XmlNodeType枚举

这样做。


为了使nav.InsertAfter(newNav)操作成功,两个

导航器应该放在文档的有意义的项目上。在

示例代码中,导航器位于相应的文档上。就像在XmlDocument中的
一样,你不能在文档之后插入文档。大多数

您可能希望导航器位于相应的文档内:

static void Main()

{

XmlDocument docNav = new XmlDocument();

docNav.LoadXml("< root />");

XPathNavigator nav = docNav.DocumentElement.CreateNavigator() ;


XmlDocument doc = new XmlDocument();

doc.LoadXml("< DocumentData>< / DocumentData>");

XmlElement elem = doc.CreateElement(" element");

elem.AppendChild(doc.CreateCDataSection(" cdata"));

doc.DocumentElement.AppendChild(elem);

XPathNavigator newNav = doc.DocumentElement.CreateNavigator();


nav.AppendChild(newNav);

}


一旦你做到这一点,你会注意到

XPath数据模型中缺少CDATA部分会阻止你通过导航器'new'的

遍历来创建CDATA部分导航。如果你使用XmlWriter作为AppendChild()的输入,你可以解决这个缺点:

使用(XmlWriter writer = nav.AppendChild()){

writer.WriterCData(" ...");

}

CDATA部分保存在XmlDocument中并且是可观察的
通过DOM API
。但是,通过XPathNavigator接口,CDATA

部分将显示为Text。


Ion


" ericms" < ER **** @ discussions.microsoft.com>在留言中写道

新闻:A5 ********************************** @ microsof t.com ...

有人能告诉我如何使用XPathNavigator插入CDATA部分吗?我没试过没试过:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator ();

XmlDocument doc = new XmlDocument();
doc.LoadXml("< DocumentData>< / DocumentData>");
XmlElement elem = doc .CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);

问候,
Eric .-



这里是我试过的:


使用(XmlWriter writer = nav.AppendChild())

{

writer.WriteCData(" ;<![CDATA [" + dataString +"]]>");

}


这是我得到的:


< DocumentData><![CDATA [Mzc4MDY4NzA ....]]>< / DocumentData> ;


我认为WriteCData应该保持字符串不变。


感谢您的评论。

Eric.-

" Ion Vasilian"写道:

嗨Eric,

当您通过XPathNavigator界面修改XmlDocument时,您需要注册
以使用XPath数据模型与XmlDocument略有不同。其中一个不同之处在于缺少CDATA部分,这些部分与任何其他纯文本一样被处理。请注意,XPathNodeType枚举
没有CDATA部分的对应字段,而XmlNodeType枚举确实没有。

为了nav.InsertAfter(newNav)操作为了取得成功,两个导航器应该放在文档的有意义的项目上。在
示例代码中,导航器位于相应的文档上。就像在XmlDocument中一样,你不能在文档之后插入文档。大多数
你可能希望导航器位于相应的文档中:
static void Main()
{XmlDocument docNav = new XmlDocument();
docNav.LoadXml( "< root />");
XPathNavigator nav = docNav.DocumentElement.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml (< DocumentData>< / DocumentData>");
XmlElement elem = doc.CreateElement(" element");
elem.AppendChild(doc.CreateCDataSection(" cdata")) );
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.DocumentElement.CreateNavigator();

nav.AppendChild(newNav);
}

一旦你做到这一点,你会发现在/或XPath数据模型中缺少CDATA部分会阻止你创建CDATA部分通过浏览器newNav的遍历。如果你使用XmlWriter作为AppendChild()的输入,你可以解决这个缺点:
使用(XmlWriter writer = nav.AppendChild()){
writer.WriterCData("。 ..);
}
CDATA部分保存在XmlDocument中,并且可以通过DOM API进行观察。但是,通过XPathNavigator接口,CDATA
部分将显示为文本。

离子

ericms < ER **** @ discussions.microsoft.com>在消息中写道
新闻:A5 ********************************** @ microsof t.com。 ..

有人能告诉我如何使用XPathNavigator插入CDATA部分吗?我没试过没试过:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator ();

XmlDocument doc = new XmlDocument();
doc.LoadXml("< DocumentData>< / DocumentData>");
XmlElement elem = doc .CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);

问候,
Eric .-




不知何故,当我在这里粘贴它时,看起来没问题,但实际上它在记事本中看起来像




< ; DocumentData><![CDATA [Mzc4MDY4Nz ....]]>< / DocumentData>


" ericms"写道:

这是我尝试过的:

使用(XmlWriter writer = nav.AppendChild())
{
作者。 WriteCData("<![CDATA [" + dataString +"]]>");
}

这就是我得到的:

< DocumentData><![CDATA [Mzc4MDY4NzA ....]]>< / DocumentData>

我认为WriteCData应该保持字符串不变。

感谢您提出任何意见。
Eric.-

" Ion Vasilian"写道:

嗨Eric,

当您通过XPathNavigator界面修改XmlDocument时,您需要注册
以使用XPath数据模型与XmlDocument略有不同。其中一个不同之处在于缺少CDATA部分,这些部分与任何其他纯文本一样被处理。请注意,XPathNodeType枚举
没有CDATA部分的对应字段,而XmlNodeType枚举确实没有。

为了nav.InsertAfter(newNav)操作为了取得成功,两个导航器应该放在文档的有意义的项目上。在
示例代码中,导航器位于相应的文档上。就像在XmlDocument中一样,你不能在文档之后插入文档。大多数
你可能希望导航器位于相应的文档中:
static void Main()
{XmlDocument docNav = new XmlDocument();
docNav.LoadXml( "< root />");
XPathNavigator nav = docNav.DocumentElement.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml (< DocumentData>< / DocumentData>");
XmlElement elem = doc.CreateElement(" element");
elem.AppendChild(doc.CreateCDataSection(" cdata")) );
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.DocumentElement.CreateNavigator();

nav.AppendChild(newNav);
}

一旦你做到这一点,你会发现在/或XPath数据模型中缺少CDATA部分会阻止你创建CDATA部分通过浏览器newNav的遍历。如果你使用XmlWriter作为AppendChild()的输入,你可以解决这个缺点:
使用(XmlWriter writer = nav.AppendChild()){
writer.WriterCData("。 ..);
}
CDATA部分保存在XmlDocument中,并且可以通过DOM API进行观察。但是,通过XPathNavigator接口,CDATA
部分将显示为文本。

离子

ericms < ER **** @ discussions.microsoft.com>在消息中写道
新闻:A5 ********************************** @ microsof t.com。 ..

有人能告诉我如何使用XPathNavigator插入CDATA部分吗?我没试过没试过:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator ();

XmlDocument doc = new XmlDocument();
doc.LoadXml("< DocumentData>< / DocumentData>");
XmlElement elem = doc .CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);

问候,
Eric .-




Can anybody show me how to insert a CDATA section using XPathNavigator ? I
have tried the follwing with no luck:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString ));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);
Regards,
Eric.-

解决方案

Hi Eric,

When you modify a XmlDocument via the XPathNavigator interface you sign up
for working with the XPath data model which is slightly different than the
XmlDocument. One such difference is the lack of CDATA sections which are
treated like any other plain text. Notice that the XPathNodeType enum
doesn''t have a corresponding field for CDATA sections vs. XmlNodeType enum
which does.

In order for the nav.InsertAfter(newNav) operation to be successful, both
navigators should be positioned on meaningful items of the document. In the
sample code the navigators are positioned on the respective documents. Just
like in XmlDocument, you can''t insert a document after a document. Most
likely you want the navigators positioned inside the respective documents:
static void Main()
{
XmlDocument docNav = new XmlDocument();
docNav.LoadXml("<root/>");
XPathNavigator nav = docNav.DocumentElement.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement("element");
elem.AppendChild(doc.CreateCDataSection("cdata"));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.DocumentElement.CreateNavigator();

nav.AppendChild(newNav);
}

Once you get this far, you''ll notice that the lack of CDATA sections in the
XPath data model prevents you from creating the CDATA sections through the
traversal of the navigator ''newNav''. You can get around this shortcoming if
you use a XmlWriter as input for the AppendChild():
using (XmlWriter writer = nav.AppendChild()) {
writer.WriterCData("...");
}
The CDATA sections are persisted in the XmlDocument and are observable
through the DOM API. However, through the XPathNavigator interface the CDATA
sections will be exposed as Text.

Ion

"ericms" <er****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...

Can anybody show me how to insert a CDATA section using XPathNavigator ? I
have tried the follwing with no luck:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString ));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);
Regards,
Eric.-



Here is what I tried:

using (XmlWriter writer = nav.AppendChild())
{
writer.WriteCData("<![CDATA[" + dataString + "]]>");
}

and here is what I got:

<DocumentData><![CDATA[Mzc4MDY4NzA....]]></DocumentData>

I thought that WriteCData should have kept the string as is.

Thanks for any comments.
Eric.-
"Ion Vasilian" wrote:

Hi Eric,

When you modify a XmlDocument via the XPathNavigator interface you sign up
for working with the XPath data model which is slightly different than the
XmlDocument. One such difference is the lack of CDATA sections which are
treated like any other plain text. Notice that the XPathNodeType enum
doesn''t have a corresponding field for CDATA sections vs. XmlNodeType enum
which does.

In order for the nav.InsertAfter(newNav) operation to be successful, both
navigators should be positioned on meaningful items of the document. In the
sample code the navigators are positioned on the respective documents. Just
like in XmlDocument, you can''t insert a document after a document. Most
likely you want the navigators positioned inside the respective documents:
static void Main()
{
XmlDocument docNav = new XmlDocument();
docNav.LoadXml("<root/>");
XPathNavigator nav = docNav.DocumentElement.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement("element");
elem.AppendChild(doc.CreateCDataSection("cdata"));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.DocumentElement.CreateNavigator();

nav.AppendChild(newNav);
}

Once you get this far, you''ll notice that the lack of CDATA sections in the
XPath data model prevents you from creating the CDATA sections through the
traversal of the navigator ''newNav''. You can get around this shortcoming if
you use a XmlWriter as input for the AppendChild():
using (XmlWriter writer = nav.AppendChild()) {
writer.WriterCData("...");
}
The CDATA sections are persisted in the XmlDocument and are observable
through the DOM API. However, through the XPathNavigator interface the CDATA
sections will be exposed as Text.

Ion

"ericms" <er****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...

Can anybody show me how to insert a CDATA section using XPathNavigator ? I
have tried the follwing with no luck:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString ));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);
Regards,
Eric.-




Somehow, when I paste it here, it looks ok, but actually it looks like the
following in Notepad:

<DocumentData><![CDATA[Mzc4MDY4Nz....]]></DocumentData>


"ericms" wrote:

Here is what I tried:

using (XmlWriter writer = nav.AppendChild())
{
writer.WriteCData("<![CDATA[" + dataString + "]]>");
}

and here is what I got:

<DocumentData><![CDATA[Mzc4MDY4NzA....]]></DocumentData>

I thought that WriteCData should have kept the string as is.

Thanks for any comments.
Eric.-
"Ion Vasilian" wrote:

Hi Eric,

When you modify a XmlDocument via the XPathNavigator interface you sign up
for working with the XPath data model which is slightly different than the
XmlDocument. One such difference is the lack of CDATA sections which are
treated like any other plain text. Notice that the XPathNodeType enum
doesn''t have a corresponding field for CDATA sections vs. XmlNodeType enum
which does.

In order for the nav.InsertAfter(newNav) operation to be successful, both
navigators should be positioned on meaningful items of the document. In the
sample code the navigators are positioned on the respective documents. Just
like in XmlDocument, you can''t insert a document after a document. Most
likely you want the navigators positioned inside the respective documents:
static void Main()
{
XmlDocument docNav = new XmlDocument();
docNav.LoadXml("<root/>");
XPathNavigator nav = docNav.DocumentElement.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement("element");
elem.AppendChild(doc.CreateCDataSection("cdata"));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.DocumentElement.CreateNavigator();

nav.AppendChild(newNav);
}

Once you get this far, you''ll notice that the lack of CDATA sections in the
XPath data model prevents you from creating the CDATA sections through the
traversal of the navigator ''newNav''. You can get around this shortcoming if
you use a XmlWriter as input for the AppendChild():
using (XmlWriter writer = nav.AppendChild()) {
writer.WriterCData("...");
}
The CDATA sections are persisted in the XmlDocument and are observable
through the DOM API. However, through the XPathNavigator interface the CDATA
sections will be exposed as Text.

Ion

"ericms" <er****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...

Can anybody show me how to insert a CDATA section using XPathNavigator ? I
have tried the follwing with no luck:

XmlDocument docNav = new XmlDocument();
docNav.LoadXml(xmlString);
XPathNavigator nav = docNav.CreateNavigator();

XmlDocument doc = new XmlDocument();
doc.LoadXml("<DocumentData></DocumentData>");
XmlElement elem = doc.CreateElement(currentNodeName);
elem.AppendChild(doc.CreateCDataSection(dataString ));
doc.DocumentElement.AppendChild(elem);
XPathNavigator newNav = doc.CreateNavigator();

nav.InsertAfter(newNav);
Regards,
Eric.-




这篇关于如何使用XPathNavigator插入CDATA部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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