'xsi' is an undeclared prefix using XmlDocument

后端 未结 2 694
借酒劲吻你
借酒劲吻你 2020-12-11 01:16

I am receiving \'xsi\' is an undeclared prefix using XmlDocument.

I am trying to read a file which has the following schema:



        
相关标签:
2条回答
  • 2020-12-11 01:36

    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);
    
    ..
    
    0 讨论(0)
  • 2020-12-11 01:40

    You are missing the xsinamespace 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>
    
    0 讨论(0)
提交回复
热议问题