2

I got this error message when I try to read the XMLfile

 Unhandled Exception: System.Xml.XmlException: 'xi' is an undeclared namespace. Line 12, position 18.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg, Int32 lineNo, Int32 linePos)
   at System.Xml.XmlTextReaderImpl.LookupNamespace(NodeData node)
   at System.Xml.XmlTextReaderImpl.ElementNamespaceLookup()
   at System.Xml.XmlTextReaderImpl.ParseAttributes()
   at System.Xml.XmlTextReaderImpl.ParseElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()

Code to read XML:

   XmlTextReader reader = new XmlTextReader("file.xml");
    while (reader.Read())
    {
        // Do some work here on the data.
        Console.WriteLine(reader.Name);
    }
    Console.ReadLine();

The xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>    
<doh> 
        <!-- Available Resources-->
        <servers>
                <xi:include href="file.xml"/>
        </servers>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
John Ryann
  • 2,283
  • 11
  • 43
  • 60
  • 2
    Your XML is invalid, there is no `xi` namespace defined. – Konrad Kokosa Dec 04 '13 at 23:18
  • 1
    xi is an alias to a namespace, but the xmlns:{alias}={namespace-uri} is not defined. So, you are using an 'undeclared prefix' – Marvin Smit Dec 04 '13 at 23:19
  • I have no choice. I must use the xml file as is. is there a trick? or workaround to ignore xi? – John Ryann Dec 04 '13 at 23:28
  • 1
    FYI, you should not use `new XmlTextReader()` or `new XmlTextWriter()`. They have been deprecated since .NET 2.0. Use `XmlReader.Create()` or `XmlWriter.Create()` instead. – John Saunders Dec 05 '13 at 00:30
  • You're right - you have no choice. You will never make this work. The XML is simply not valid. It's not a matter of your lack of knowledge - the XML is simply garbage. The only way you will meet the requirement to read this file is if the people who created the file change it. Otherwise, time to find a new job. – John Saunders Dec 05 '13 at 00:31
  • @JohnRyann, if someone requires to process such XML, internally pre-process it before reading to simply add missing namespace. – Konrad Kokosa Dec 05 '13 at 00:34
  • See this suggestion, turning off namespaces using an XMLTextReader http://stackoverflow.com/a/4039678/655625 – Chris Amelinckx Sep 22 '15 at 15:26

1 Answers1

4

I assume that you are wanting to use XInclude. You must define which namespace the xi prefix maps to. The easiest why is too include the namespace mapping in the root element.

  <doh xmlns:xi="http://www.w3.org/2001/XInclude">

Note that the XmlTextReader will not include the "file.xml". You will have to 'include' by some other code. A good background paper on the XInclude can be found on MSDN at http://msdn.microsoft.com/en-us/library/aa302291.aspx

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • I can not change the xml file. it is a requirement to use it as is. is there a way to ignore it? consider it as a comment? – John Ryann Dec 04 '13 at 23:27
  • 1
    The XML that you show is wrong it is not valid XML. All xml readers in any language will report an error. You should go back to the producer of the XML and get them to produce correct XML. – Richard Schneider Dec 05 '13 at 00:07