我收到'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>
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
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
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释该行以忽略命名空间,它会加载正常但无法保存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">
Run Code Online (Sandbox Code Playgroud)
更新#1 我尝试了以下内容:
XmlDocument xmldoc = new XmlDocument();
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("xsi", …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用XmlNamespaceManager将命名空间添加到XmlDocument.这是当前的xml:
<?xml version="1.0"?>
<kml>
<Document>
<Placemark>
</Placemark>
</Document>
</kml>
Run Code Online (Sandbox Code Playgroud)
我希望它转换为这个xml(使用XmlNamespaceManager):
<?xml version="1.0"?>
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Document>
<Placemark>
</Placemark>
</Document>
</kml>
Run Code Online (Sandbox Code Playgroud)
但我无法更改xml.这是代码,我知道它应该是一个简单的修复:
public void addXmlns()
{
string xml = @"<?xml version=""1.0""?>
<kml>
<Document>
<Placemark>
</Placemark>
</Document>
</kml>";
var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
//Add the namespaces
nsmgr.AddNamespace("", "http://www.opengis.net/kml/2.2");
nsmgr.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
nsmgr.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string message;
message = xmldoc.InnerXml;
MessageBox.Show(message); // still shows the original xml
}
Run Code Online (Sandbox Code Playgroud)
先谢谢你
更新#1 - 试过这个,但它也不会改变XML: …
我正在尝试使用HtmlAgilityPack更新外部Html.该属性显示为只读.我的问题是如何更新外部Html?注意:我确实需要更新外部html(不仅仅是内部html).这是代码:
// Check if there is a nested table
HtmlAgilityPack.HtmlNode nestedtable = tr.SelectSingleNode(".//table");
if (nestedtable != null)
{
// Save Inner/Outer Html and update Outer Html
string strInnerHtml = nestedtable.InnerHtml;
string strOuterHtml = nestedtable.OuterHtml;
string strNewOuterHtml = "<table><tr><td><table><tr><td>inner1update</td><td>inner2update</td></tr></table></td></tr></table>";
// Now update source HtmlDocument
nestedtable.OuterHtml = strNewOuterHtml;
// ^^^ Error line: Property or indexer
//HtmlAgilityPack.HtmlNode.OuterHtml' cannot be assigned to -- it is read only
}
Run Code Online (Sandbox Code Playgroud)