开发者

Inserting XML Fragment after last specific node/element

I want to add an XML fragment to the last element to an XML document and I having probl开发者_开发技巧ems i.e. the error I get is:

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

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>

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> 

The code I am using is:

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.

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.

UPDATE/SOLUTION - How to do this with XPATH

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 :)


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);

This results in:

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


Things look pretty good, but I would first try to avoid the xpath selection for the last node, and instead just use this:

rootElement.InsertAfter(xmlDocFrag, rootElement.LastChild);


I had similar issue, I used the ImportNode method to solve it

Here is a small example how you can use it to add node from different xml (stored in string) to your example at desired node in xml tree

string xmlstring =@"<tag>.....</tag>"; // holds xml tree to be appended
       XmlDocument xml2 = new XmlDocument();
        xml2.Load(@"path_of_main_xml");

        XmlDocument xml1 = new XmlDocument();
        xml1.Load(new StringReader(xmlString));

        // get the node you want to import which in this icase is string
        XmlNode elem = xml1.DocumentElement;
 // use importNode to import it
        XmlNode impnode = xml2.ImportNode(elem,true);
 // get the node list of all node of particular tag name
        XmlNodeList eNode = xml2.GetElementsByTagName("tag_name_of_parent");
        eNode[0].AppendChild(impnode); // append new node
// write back the updates to same file
        XmlWriter writer = XmlWriter.Create(@"path_of_main_xml");
        xml2.Save(writer); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜