I'm attempting to add a new node to an existing xml file. The file has the xi namespace defined:
<Module xmlns:xi="http://www.w3.org/2001/XInclude" ...
and contains nodes with:
<xi:include href="somefile.xml" />
This loads fine, and I can operate on the existing nodes, but if I try to add a new node like so:
XmlDocumentFragment frag = doc.CreateDocumentFragment();
frag.InnerXml =
"<SomeNode SomeAttribute=\"\">" +
" <xi:include href=\"SomeFile.xml\" />" +
"</SomeNode>";
I get an XmlException for "'xi' is an undeclared prefix"
I presume it is unhappy that the fragment itself does not recognize the namespace, but I'm not seeing any intuitive way to fix this.
答案 0 :(得分:0)
As I was writing this question, I had an idea that worked and am posting it in case it helps someone else. I decided to add the offending xml after adding the fragment to the document:
XmlDocumentFragment frag = doc.CreateDocumentFragment();
frag.InnerXml =
"<SomeNode SomeAttribute=\"\">" +
"</SomeNode>";
XmlNode newNode = parentNode.AppendChild(frag);
newNode.InnerXml = "<xi:include href=\"SomeFile.xml\" />";
If someone has a cleaner way to do this. I am interested to hear what it is.
答案 1 :(得分:0)
在<xi:include href="somefile.xml" />
中,xi
是名称空间前缀。
如果使用,则必须声明命名空间前缀。您必须添加使用名称空间前缀xi
的元素或其上方,其声明:
xmlns:xi="http://www.w3.org/2001/XInclude"
如果您temporarily managed to skirt an error message by assembling fragments in a certain manner,请认识到最终结果是重要的:如果使用了名称空间但未声明,则仍然存在问题。