簡體   English   中英

“ xsi”是使用XmlDocument的未聲明的前綴

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

我使用XmlDocument收到未聲明的前綴“ xsi”。

我正在嘗試讀取具有以下架構的文件:

<?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>

我嘗試了以下方法:

    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
    }

如果取消注釋該行以忽略命名空間,則該行可以正常加載,但以后無法保存XmlDocument 因此,忽略它不是解決方案。 有誰知道如何正確加載架構? 問題/錯誤似乎在此節點中:

<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">

更新#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

但是現在正在收到此錯誤:

“無法將指定的節點作為該節點的有效子節點插入,因為指定的節點類型錯誤。” 看來我越來越近了...

解:

我能夠解決問題! 這是最終代碼:

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);

還有另外一個技巧,當搜索節點時,請記住設置正確的名稱空間,例如搜索上面的地標,其格式如下:

// 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);

..

您缺少xsi名稱空間聲明:

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

現在您的文檔應如下所示:

<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