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


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.


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

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", "");
            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); 
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