Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding child nodes using c# Xdocument class

Tags:

c#

xml

I have an xml file as given below.

<?xml version="1.0" encoding="utf-8"?>
 <file:Situattion xmlns:file="test">

  <file:Properties>

</file:Situattion>

I would like to add the child element file:Character using xDocument.So that my final xml would be like given below

<?xml version="1.0" encoding="utf-8"?>
  <file:Situattion xmlns:file="test">

   <file:Characters>

     <file:Character file:ID="File0">
     <file:Value>value0</file:Value>
     <file:Description>
      Description0 
     </file:Description>
     </file:Character>

 <file:Character file:ID="File1">
     <file:Value>value1</file:Value>
     <file:Description>
     Description1
     </file:Description>
     </file:Character>

     </file:Characters>

Code in c# i tried using Xdocument class is given below.

        XNamespace ns = "test";
        Document = XDocument.Load(Folderpath + "\\File.test");

        if (Document.Descendants(ns + "Characters") != null)
        {

            Document.Add(new XElement(ns + "Character"));
        }
        Document.Save(Folderpath + "\\File.test");

At line "Document.Add(new XElement(ns + "Character"));", I am getting an error:

"This operation would create an incorrectly structured document.".

How can I add the node under "file:Characters".

like image 345
mystack Avatar asked Jul 29 '13 05:07

mystack


People also ask

How do I add a child node to a selected node?

Depending on how you want it to behave, you need to be explicit about the parent node you wish to add the child to. If you want to add the children to the currently selected node, get the TreeView.SelectedNode and add the children to it. Try TreeView to get an idea of how the class operates.

How to add a node at the front of a node?

The push () must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See this) Following are the 4 steps to add a node at the front. Time complexity of push () is O (1) as it does a constant amount of work. We are given a pointer to a node, and the new node is inserted after the given node.

How do I move a node from one parent to another?

If the current node is null then create and insert the new node there and make it as one of the children of the parent/previous node depending on its value. If the value of current node is less than the new value then move to the right child of current node else move to the left child.

How to add a node to a linked list?

A node can be added in three ways 2) After a given node. 3) At the end of the linked list. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution.


1 Answers

You're trying to add an extra file:Character element directly into the root. You don't want to do that - you want to add it under the file:Characters element, presumably.

Also note that Descendants() will never return null - it will return an empty sequence if there are no matching elements. So you want:

var ns = "test";
var file = Path.Combine(folderPath, "File.test");
var doc = XDocument.Load(file);
// Or var characters = document.Root.Element(ns + "Characters")
var characters = document.Descendants(ns + "Characters").FirstOrDefault();
if (characters != null)
{
    characters.Add(new XElement(ns + "Character");
    doc.Save(file);
}

Note that I've used more conventional naming, Path.Combine, and also moved the Save call so that you'll only end up saving if you've actually made a change to the document.

like image 108
Jon Skeet Avatar answered Sep 19 '22 11:09

Jon Skeet