我收到'xsi'是使用XmlDocument的未声明的前缀。
我正在尝试读取具有以下架构的文件:
<?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
但我现在收到此错误:
“无法将指定的节点作为此节点的有效子节点插入,因为指定的节点类型错误。”看起来我越来越近了......
答案 0 :(得分:12)
<强>解决方案:强>
我能够解决问题!这是最终的代码:
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);
..
答案 1 :(得分:9)
您缺少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>