I am receiving \'xsi\' is an undeclared prefix using XmlDocument.
I am trying to read a file which has the following schema:
Solution:
I was able to solve the problem! Here is the final code:
XmlDocument xmldoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings { NameTable = new NameTable() };
XmlNamespaceManager xmlns = new XmlNamespaceManager(settings.NameTable);
xmlns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlParserContext context = new XmlParserContext(null, xmlns, "", XmlSpace.Default);
XmlReader reader = XmlReader.Create(strXmlFile, settings, context);
xmldoc.Load(reader);
Also one more tip, when searching through the nodes, remember to set the correct namespace, for example to search for Placemark above, this would be the format:
// Setup default namespace manager for searching through nodes
XmlNamespaceManager manager = new XmlNamespaceManager(xmldoc.NameTable);
string defaultns = xmldoc.DocumentElement.GetNamespaceOfPrefix("");
manager.AddNamespace("ns", defaultns);
// get a list of all <Placemark> nodes
XmlNodeList listOfPlacemark = xmldoc.SelectNodes("//ns:Placemark", manager);
// iterate over the <Placemark> nodes
foreach (XmlNode singlePlaceMark in listOfPlacemark)
// Get the description subnode
XmlNode descriptionNode = singlePlaceMark.SelectSingleNode("ns:description", manager);
..
You are missing the xsi
namespace declaration:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
now your document should look something like this:
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:atom="http://www.w3.org/2005/Atom">
.....
</kml>