0

I have a string in XML format returned by a without RootNode as follows:

<Node1 Id = "1" Value = "a"/>
<Node2 Id = "2" Value = "b"/>
<Node3 Id = "3" Value = "c"/>

Now, what I want is to embed the returned string in a root node, for example

<XML>
   <Node1 .... />
   <Node2 .... />
</XML>

how can I do this using System.Xml namespace (XmlDocument, ecc..) ?

I have following code:

XmlDocument xml = new XmlDocument(); 
xml.Loadxml(stringWithoutRoot); 

Thank you in advance.

EDIT: ok sorry I was unclear. My real problem is this. I have a lot of methods doing practically the same thing, the only thing that changes is the name of the stored procedure that must be executed. So what I want to do to make the code more readable is to create only one method and pass the name of the stored procedure as its input parameter. I think this is more logic.

Second issue: I noticed that some stored procedures return an xml with root node and others don't so my solution is to pass also the XML as input parameter.

An example invoking,

executeSqlCommand(mySP, "<XML></XML>)

will put the result of the stored procedure in the XML as follows

<XML>resultFromSPWithoutRootNode</XML>

so I need something like this

string result = ..... // some operation in DB

if (!string.IsNullOrEmpty(rootXMl))   // where rootXML is my second parameter
{
    append the result to the root node rootXML
}

The append way is missing.

2
  • Where is your code? Commented Mar 27, 2017 at 14:47
  • I've no code. What I do: XmlDocument xml = new XmlDocument(); xml.Loadxml(stringWithoutRoot); I don't know how to append my sting to a root node. Commented Mar 27, 2017 at 14:56

2 Answers 2

1

maybe something like that ?

XmlDocument xml = new XmlDocument();
string nodesString = "<node/>" ;
xml.LoadXml("<root>" + nodesString+ "</root>");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jérôme for your tip. Please see my EDIT to better understand my question. I need something like this: xml.LoadXml(rootNode); xml.Append(restOfXml)
0

I will try to use XmlDocumentFragment like suggested here:

Append XML string block to existing XmlDocument

Thank you

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.