Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring namespace in XmlReader

Tags:

c#

xml

xmlreader

I'm using an xmlreader to read an xml file. The problem is i have many undefined namespaces in the child elements. Because of it, I'm unable to read the content of the files. Is there any way to read the contents of the files avoiding this issue or is there any solution to handle these kind of scenarios?

like image 799
senthil kumar Avatar asked Dec 04 '25 08:12

senthil kumar


1 Answers

You can add the missing namespaces to the XmlReader like this.

var settings = new XmlReaderSettings
{
    NameTable = new NameTable(),
};
XmlNamespaceManager xmlns = new XmlNamespaceManager(settings.NameTable);
xmlns.AddNamespace("yourundeclarednamespace", "http://www.dummynamespace.org");
XmlParserContext context = new XmlParserContext(null, xmlns, "", XmlSpace.Default);
using (var reader = XmlReader.Create(filePath, settings, context))
{
}
like image 197
Eric Avatar answered Dec 05 '25 22:12

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!