0

I'm trying to create an XML with multiple root elements. I can't change that because that is the way I'm supposed to send the XML to the server. This is the error I get when I try to run the code:

System.InvalidOperationException: This operation would create an incorrectly structured document.

Is there a way to overwrite this error and have it so that it ignores this?

Alright so let me explain this better:

Here is what I have

XmlDocument doc = new XmlDocument();
doc.LoadXml(_application_data);

Now that creates the XML document and I can add a fake root element to it so that it works. However, I need to get rid of that and convert it into a DocumentElement object.

How would I go about doing that?

4
  • 1
    Create a root that encompasses the multiples and then submit only the inner xml. Commented Oct 24, 2012 at 22:41
  • There's no way to create an XML with multiple roots unless you do it manually. You could wrap your "root" elements in an element, and then select the children. Commented Oct 24, 2012 at 22:41
  • 1
    @dbaseman, not exactly true. Using ConformanceLevel.Fragment let one create such documents (not valid XML, but sometimes needed, i.e. when doing XML-like logging). Commented Oct 24, 2012 at 22:45
  • @AustinSalonen, How can I select the inner XML and convert it into a documentElement object? Commented Oct 24, 2012 at 23:17

3 Answers 3

4

Specify Fragment when creating XmlWriter as shown here

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

// Create the XmlWriter object and write some content.
MemoryStream strm = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(strm, settings))
{
    writer.WriteElementString("orderID", "1-456-ab");
    writer.WriteElementString("orderID", "2-36-00a");
    writer.Flush();
}
Sign up to request clarification or add additional context in comments.

Comments

0

If it has multiple root elements, it's not XML. If it resembles XML in other ways, you could place everything under a root element, then when you send the string to the server, you just combine the serialized child elements of this root element, or as @Austin points out, use an inner XML method if available.

Comments

0

just create an XML with single root then get it's content as XML text.

you are talking about XML fragment anyways, since good xml has only one root.

this is sample to help you started:

var xml = new XmlDocument();
var root = xml.CreateElement("root");
root.AppendChild(xml.CreateElement("a"));
root.AppendChild(xml.CreateElement("b"));
Console.WriteLine(root.InnerXml); // outputs "<a /><b />"

1 Comment

I tried this but it still contains the root element. Also, the innerxml is a string and I need it as a documentelement object

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.