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 340 of 21350

Undeclared Namespace in XML (eg: xsi is an undeclared namespace)

Blogger : MSDN Blogs
All posts : All posts by MSDN Blogs
Category : .NET XML, System.XML
Blogged date : 2009 Feb 13

If you XML source publishes invalid XML for some reason ( i.e. it has Xml elements referring to undeclared namespace ) and you try to load this XML in XMLDocument. You would end up getting exception similar to <NameSpace> is an undeclared namespace.

eg:

<?xml version="1.0" encoding="utf-16"?>
<root>
<Name xsi:nil="true">name1</Name>
<Name xsi:nil="true">name2</Name>
<Name xsi:nil="true">name3</Name>
</root>

Ideally this is an Invalid XML but there is a way to load it in XMLDocument using following code snippet.

            XmlDocument doc = new XmlDocument();
            
            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
            XmlReaderSettings xset = new XmlReaderSettings();
            xset.ConformanceLevel = ConformanceLevel.Fragment;
            XmlReader rd = XmlReader.Create(new  StringReader(XMLSource), xset, context); 
              
            doc.Load(rd);
Runeet Vashisht

Read comments or post a reply to : Undeclared Namespace in XML (eg: xsi is an undeclared namespace)
Page 340 of 21350

Newest posts
 

    Email TopXML