11

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".

2
  • have you looked into using XPATH or XQuery also look at the ->Related Link located to the right side of this current page, lots of working examples for you to investigate Commented Jul 29, 2013 at 5:46
  • 1
    @DJKRAZE: There's no need to use XPath or XQuery here at all, and I don't believe they'd even make the code simpler. Commented Jul 29, 2013 at 5:49

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What is the type of "document" object.?
getting an error "The ':' character, hexadecimal value 0x3A, cannot be included in a name."
@user1654136: doc is XDocument. And if you're getting that error, that suggests you're using "file:Character" as the local name instead of ns + "Character", or something similar. You shouldn't have that problem in the code I've provided.
How can i add file:ID="File0" and <file:Value>value0</file:Value> using the above code
@user1654136: Use new XAttribute(ns + "ID", "File0") and new XElement(ns + "Value", "value0"). Basically you use ns for the namespace - you don't put the alias into the localname.
7
    Document.Root.Element("Characters").Add(new XElement("Character", new XAttribute("ID", "File0"), new XElement("Value", "value0"), new XElement("Description")),
        new XElement("Character", new XAttribute("ID", "File1"), new XElement("Value", "value1"), new XElement("Description")));

Note: I have not included the namespace for brevity. You have to add those.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.