简体   繁体   English

“ xsi”是使用XmlDocument的未声明的前缀

[英]'xsi' is an undeclared prefix using XmlDocument

I am receiving 'xsi' is an undeclared prefix using XmlDocument. 我使用XmlDocument收到未声明的前缀“ xsi”。

I am trying to read a file which has the following schema: 我正在尝试读取具有以下架构的文件:

<?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 have 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 line to ignore the namespace, it loads ok but fails to save the XmlDocument later on. 如果取消注释该行以忽略命名空间,则该行可以正常加载,但以后无法保存XmlDocument So ignoring it would not be a solution. 因此,忽略它不是解决方案。 Does anyone know how to properly load the schema? 有谁知道如何正确加载架构? The issue/error appears to be 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: 更新#1我尝试了以下操作:

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 am receiving this error now: 但是现在正在收到此错误:

"The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type." “无法将指定的节点作为该节点的有效子节点插入,因为指定的节点类型错误。” It looks like I am getting closer... 看来我越来越近了...

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: 您缺少xsi名称空间声明:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM