"xsi" - undeclared prefix using XmlDocument

I get "xsi" - an undeclared prefix using XmlDocument.

I am trying to read a file that has the following diagram:

<?xml version="1.0" encoding="UTF-8"?> <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:atom="http://www.w3.org/2005/Atom"> <Document> <Document id="robert" xsi:schemaLocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd"> <Placemark> <description>test</description> </Placemark> </Document> </Document> </kml> 

I tried the following:

  XmlDocument xmldoc = new XmlDocument(); using (XmlTextReader tr = new XmlTextReader(strXmlFile)) { //tr.Namespaces = false; (uncomment to ignore namespace) xmldoc.Load(tr); // 'xsi' is an undeclared prefix error here } 

If I uncomment the string to ignore the namespace, it loads normally, but does not save the XmlDocument later. Therefore, ignoring this would not be a solution. Does anyone know how to load the schema correctly? Error / error in this node:

 <Document id="robert" xsi:schemaLocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd"> 

Update # 1 I tried the following:

 XmlDocument xmldoc = 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(strXmlFile), xset, context); xmldoc.Load(rd); // error is still on this line 

But I get this error now:

"The specified node cannot be inserted as a valid child of this node because the specified node is the wrong type." Looks like I'm getting closer ...

+8
c # xml parsing
source share
2 answers

Decision:

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 for nodes, do not forget to set the correct namespace, for example, to search for the label label above, this will 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); .. 
+9
source share

You are missing the xsi namespace declaration:

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

Your document should now 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> 
+7
source share

All Articles