2

I want to add an XML string as new node to an existing XML document.

For example, suppose the input from the user is:

<bk:book>
  <title>Pride And Prejudice</title>
  <authorlastname>Jane</authorlastname>
  <authorfirstname>Austen</authorfirstname>
  <price>24.95</price>
</bk:book>

I am trying to insert that user input as follows:

xml_SourceDoc.Root.LastNode.AddAfterSelf(XElement.Parse(xmlString));

However, that statement is raising this exception:

bk is an undeclared prefix. Line 1, position 2.

How can I change my approach so that I can successfully insert any text that is input by the user?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
user2822362
  • 127
  • 1
  • 13
  • 1
    `bk` _is_ an undefined prefix. You're getting that exception from `XElement.Parse`. It has nothing to do with the fact that you're trying to add the node to the end. – John Saunders Sep 27 '13 at 07:35
  • "It has nothing to do with the fact that you're trying to add the node to the end. " But I'm receiving the exceptoin as mentioned above. to add as a node to rood, I need parse the string as XElement. – user2822362 Sep 27 '13 at 07:46
  • 1
    It has to do with the fact that you're attempting to parse invalid XML. It would happen even if you made no attempt to add the invalid XML to the end. – John Saunders Sep 27 '13 at 07:50
  • How a piece of xml String can be invalid, consider example shown. IF I remove bk: for testing, the code works. with bk: it's throwing exception. What could be the reason? – user2822362 Sep 27 '13 at 08:00
  • 2
    The reason is that it is invalid XML. It is a namespace prefix without a declaration. This is simply user error: the user entered invalid XML. – John Saunders Sep 27 '13 at 08:01

3 Answers3

2

If you really don't know what the user will enter, you can simply handle it as CDATA via the LINQ to XML XCData Class.

Here is what your sample data would look like when inserted as a node in a container XML document:

<doc>
  <content><![CDATA[<bk:book>
   <title>Pride And Prejudice</title>
   <authorlastname>Jane</authorlastname>
   <authorfirstname>Austen</authorfirstname>
   <price>24.95</price>
 </bk:book>]]></content>
</doc>

And here is an example program that creates the above example document:

using System;
using System.Xml;
using System.Xml.Linq;

public class CDataExample
{
    public static void Main()
    {
        string documentXml = "<doc><content></content></doc>";
        XElement doc = XElement.Parse(documentXml, LoadOptions.None);

        string userInput =
 @"<bk:book>
   <title>Pride And Prejudice</title>
   <authorlastname>Jane</authorlastname>
   <authorfirstname>Austen</authorfirstname>
   <price>24.95</price>
 </bk:book>";

        XCData cdata = new XCData(userInput);
        doc.Element("content").Add(cdata);

        Console.WriteLine(doc.ToString());
    }
}
DavidRR
  • 18,291
  • 25
  • 109
  • 191
0

Check the xml is parsable first:

Check well-formed XML without a try/catch?

if(IsValidXML(xmlString))
{
    xml_SourceDoc.Root.LastNode.AddAfterSelf(XElement.Parse(xmlString));
}
Community
  • 1
  • 1
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
  • I'm receiving same exception. Exception throws at XDocument doc = XDocument.Parse(xmlString); – user2822362 Sep 27 '13 at 07:27
  • I want to insert the xmlstring to xml file, what ever it may be. Even if there is namespace I need to insert the xml as a node to root. – user2822362 Sep 27 '13 at 07:35
  • -1 for the idea of removing the prefix without understanding what it means. – John Saunders Sep 27 '13 at 07:36
  • @JohnSaunders You cannot parse the xml so I suggested how to make it parsable. I do understand what it means. – Sam Leach Sep 27 '13 at 07:57
  • @SamLeach: maybe the OP should be processing valid XML. Also, read the question and you'll see that this is coming from user input - this isn't hard coded XML. Next time around, there could be a different prefix or multiple prefixes, or maybe even a valid namespace declaration for `bk`. Your could would make a valid namespace declaration invalid. – John Saunders Sep 27 '13 at 07:59
  • @JohnSaunders I've updated my answer suggesting OP check for well-formed xml before adding the xml. – Sam Leach Sep 27 '13 at 08:01
  • If I check this,definitely the function will return false. But somehow If I want to insert the xmlfragment. How could I do that? – user2822362 Sep 27 '13 at 09:08
  • If you want to insert the xml fragment you'll have to validate it before. In your example you remove `bk`. In other cases your validation will differ. – Sam Leach Sep 27 '13 at 09:18
0

first, create XElement you want to add:

xmlString = new XElement(new XElement("book", new XAttribute("bk) ,(new XElement("title", titleValue), new EXelement("authorlastname", authorlastNameValue ... and so on.

then add it:

xml_SourceDoc.Root.Add(xmlString);

exception you mention is caused that you do not add attribute creating XElement

Na Na
  • 818
  • 3
  • 13
  • 19