“XSI”是使用XmlDocument的未声明的前缀 [英] 'xsi' is an undeclared prefix using XmlDocument

查看:1194
本文介绍了“XSI”是使用XmlDocument的未声明的前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到XSI是使用XmlDocument的未声明的前缀

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

我想读它具有以下模式的文件:

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
    }

如果我取消注释行忽略命名空间,它加载确定,但未能挽救的XmlDocument 以后。因此忽略它不会是一个解决方案。有谁知道如何正确地加载架构?问题/错误似乎是在此节点:

If I uncomment the line to ignore the namespace, it loads ok but fails to save the XmlDocument later on. 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">



更新#1
我试过如下:

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

推荐答案

解决方案:

我是能够解决的问题!下面是最终的代码:

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

..

这篇关于“XSI”是使用XmlDocument的未声明的前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆