BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in .NET XML, System.XML

 
 
Page 2426 of 20224

Frequently Asked Questions on XML in .NET - Part 2

Blogger : MSDN Blogs
All posts : All posts by MSDN Blogs
Category : .NET XML, System.XML
Blogged date : 2008 Sep 10

This is the next post in our ongoing FAQ series. See the original post here.

Q2: Conformance Level – Fragment

There are times when you want to work with a piece of XML that is not a fully conformant xml document; an example of this is when the XML does not have one root element. This type of XML is called an XML fragment. Here is an example of an XML fragment:

“<foo></foo><bar></bar>”

If this snippet of XML is used as the source for to the XmlReader, the following exception will occur:

“There are multiple root elements. Line X, position Y.”

This is a good exception message and tells the user exactly what the problem is: more than one root element is not allowed in an XML document. But what happens if you really want to work with XML of this form? A flag in the XmlReaderSettings class is provided for exactly this situation: XmlReaderSettings.ConformanceLevel.

Code snippet to correctly read the XML fragment:

string xmlFragmentStr = "<foo></foo><bar></bar>";
//create a reader settings objects and set the conformance level to fragment
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ConformanceLevel = ConformanceLevel.Fragment;
//create a reader using the reader settings
XmlReader r = XmlReader.Create(new StringReader(xmlFragmentStr), xrs);
r.Read();

Shayne Burgess

Program Manager | Data Programmability


Read comments or post a reply to : Frequently Asked Questions on XML in .NET - Part 2
Page 2426 of 20224

Newest posts
 

    Email TopXML