开发者

XML fragment with multiple roots: suppressing VS error

In Visual Studio, I have an XML file (with .xml extension) which contains an XML fragment that I use in my program:

<tag1>
data 
...
</tag1>
<tag2>
data 
...
</tag2>

Visual Studio shows me an error in the err开发者_如何学JAVAor list: "XML document cannot contain multiple root level elements".

But this is not a complete document, just a fragment that will be reused. I want to keep my 2 roots in my fragment. How can I get rid of the error message?

Thanks


How can I get rid of the error message?

I don't think you can.

XML Documents have only a single root, there is no such thing as a "xml fragment document".

Two options:

  • Use two files, one for each fragment.
  • Put a placeholder root element in, and then use its children when using this content.

(Or put up with the error.)


I'm still looking for this answer too, but wanted to post this in case anyone stumbles across it can benefit from learning of XML Fragments.

Despite an earlier comment to the contrary, there certainly is such a thing as an XML Fragment, and .NET provides a means to work with them:

XmlDocument.CreateDocumentFragment(...).  

This document fragment can be populated with an XML Fragment that is created dynamically in memory at runtime, or one that is read from an XML Fragment file with something like File.ReadAllText("MyXMLFragment.xml").

The latter permits dynamic construction of XML documents from a store of XML Fragments on disk, and is useful for importing lists of things like Test data, or data that varies based on other factors.

The fragments on disk won't have any "

The only downside is that Studio's XML Editor tries to Validate XML, and doesn't seem to allow for XML Fragments in files without complaining.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜