在最后一个特定节点/元素之后插入XML片段 [英] Inserting XML Fragment after last specific node/element

查看:59
本文介绍了在最后一个特定节点/元素之后插入XML片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在XML文档的最后一个元素中添加XML片段,但是遇到问题,即出现的错误是:

I want to add an XML fragment to the last element to an XML document and I having problems i.e. the error I get is:

参考节点不是的子节点这个节点".

"The reference node is not a child of this node".

所以我现有的XML文档看起来像这样:

So my existing XML document looks like this:

<MAP>
  <LAYER name ="My first Layer">
    <DATASET name="foo dataset" />
    <SYMBOLOGY> 
      <SYMBOL colour="red" />
    </SYMBOLOGY>    
  </LAYER>
  <LAYER name="My second Layer">
     <DATASET name="bar dataset" /> 
     <SYMBOLOGY> 
       <SYMBOL colour="blue" />
     </SYMBOLOGY>    
  </LAYER>    
</MAP>

我要在最后一个LAYER元素之后插入的XML片段是:

The XML fragment I want to insert after the last LAYER element is:

<LAYER name="My third Layer">
     <DATASET name="whatever dataset" /> 
     <SYMBOLOGY> 
       <SYMBOL colour="yellow" />
     </SYMBOLOGY>    
</LAYER> 

我正在使用的代码是:

XmlDocumentFragment xmlDocFrag = xmlDocument.CreateDocumentFragment();
xmlDocFrag.InnerXml = inputXML; //which is basically the third layer example - see above.

XmlElement rootElement = xmlDocument.DocumentElement;
XmlNode lastLayerNode = rootElement.SelectSingleNode(@"//LAYER[last()]");

rootElement.InsertAfter(xmlDocFrag, lastLayerNode); //error raised here.

任何关于我在这里做错事情的想法都将不胜感激.我的XPath查询似乎已经找到,并且似乎出于某种奇怪的原因选择了它不会插入的正确的最后一层.

Any ideas on what I'm doing wrong here would be much appreciated. My XPath query seems find and it seems to select the correct last layer it just won't insert after it for some bizarre reason.

更新/解决方案-如何使用XPATH

最后在XPath中弄清楚了-参见下面的代码,我认为基本上是首先没有选择正确的父节点,选择最后一个LAYER然后尝试在该节点上使用InsertAfter()是不正确的.最好选择高于MAP的级别,然后再选择AppendChild().见下文:

Finally figured it out in XPath - see the code below, I think it was down to basically not selecting the correct parent node in the first place, it's incorrect to select the last LAYER then try and InsertAfter() on this node. Better to select the level above i.e. MAP then AppendChild(). See below:

XmlDocumentFragment xmlDocFrag = xmlDocument.CreateDocumentFragment();
xmlDocFrag.InnerXml = inputXML;

XmlElement mapElement = (XmlElement)xmlDocument.SelectSingleNode(@"//MAP[last()]");
mapElement.AppendChild(xmlDocFrag);

也感谢所有回复和帮助:)

Thanks to all the replies and help too :)

推荐答案

考虑到与Framework 2.0一起使用时需要的解决方案,这是另一种解决方案:

Taking into consideration that you need this to work with Framework 2.0, here's another solution:

string xml = "<map><layer>1</layer><layer>2</layer></map>";
string addMe = "<layer>3</layer>";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);

XmlDocumentFragment xmlDocFrag = xmlDocument.CreateDocumentFragment();
xmlDocFrag.InnerXml = addMe;

XmlElement rootElement = xmlDocument.DocumentElement;
rootElement.AppendChild(xmlDocFrag);

结果是:

<map><layer>1</layer><layer>2</layer><layer>3</layer></map>

这篇关于在最后一个特定节点/元素之后插入XML片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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