简体   繁体   English

带有命名空间但没有xmlns的Xml

[英]Xml with Namespace but without xmlns

I am working on a program in C# that edits open-document files on xml level. 我正在使用C#编写一个在xml级别编辑打开文档文件的程序。 For example it adds rows to tables. 例如,它向表添加行。

So I load the content.xml into an XmlDocument "doc" and traverse the xml structure. 所以我将content.xml加载到XmlDocument“doc”中并遍历xml结构。 Say I have the <table:table-row> node in an XmlNode "row" and now I want to add a <table:table-cell> node to it. 假设我在XmlNode“行”中有<table:table-row>节点,现在我想向它添加一个<table:table-cell>节点。 So I call 所以我打电话

XmlDocument doc = new XmlDocument();
doc.Load(filename);
...
XmlNode row = ...;
...
XmlNode cell = doc.CreateElement("table:table-cell");
row.Append(cell);
...
doc.Save(filename);

The problem is that, in the file, the new node only contains <table-cell>...</table-cell> 问题是,在文件中,新节点只包含<table-cell> ... </ table-cell>

C# just decides to ignore what I told it to and does something else without even telling me (at first I overlooked the problem and was wondering why it didn't work although the generated xml looked okay). C#只是决定忽略我告诉它的内容并且甚至没有告诉我做其他事情(起初我忽略了问题,并且想知道为什么它不起作用虽然生成的xml看起来没问题)。

From what I gathered out so far, the problem has to do with the fact that "table:" is a namespace. 从我到目前为止收集到的,问题与“table:”是命名空间这一事实有关。 When I also supply a NamespaceURI to CreateElement, I get <table:table-cell table:xmlns="THE_URI" >... - but the original document did not have this xmlns, so I don't want it either... 当我还向CreateElement提供NamespaceURI时,我得到<table:table-cell table:xmlns =“THE_URI”> ... - 但原始文档没有这个xmlns,所以我不想要它...

I tried to use an XmlTextWriter and setting writer.Settings.Namespaces = false, because I thought, this should suppress the output of the xmlns, but it only caused an exception - the document has some namespaces, which are forbidden if Namespaces is set to false... (wtf!? suppressing the output of xmlns seems a billion times more logical than throwing an exception if an xmlns is present...) 我试图使用XmlTextWriter并设置writer.Settings.Namespaces = false,因为我认为,这应该会抑制xmlns的输出,但它只会导致异常 - 文档有一些名称空间,如果Namespaces设置为false ...(如果存在xmlns,则wtf !?抑制xmlns的输出似乎比抛出异常的数十亿倍......)

In some similar discussions I read that you should set the cell.Name manually, but this property is read-only... Others suggest to change it on text-file level (that's tinkering and it would be slow) 在一些类似的讨论中,我读到你应该手动设置cell.Name,但是这个属性是只读的...其他人建议在文本文件级别更改它(这是修补,而且会很慢)

Can anyone give me a hint? 任何人都可以给我一个提示吗?

Every namespace should have at least one xmlns definition with a URI. 每个命名空间应至少有一个带有URI的xmlns定义。 This is the ultimate differentiation between two tags. 这是两个标签之间的最终区别。

You can however have the xmlns attribute declared only once in the file (in the beginning). 但是,您可以在文件中(在开头)仅声明一次xmlns属性。

See Creating a specific XML document using namespaces in C# 请参阅使用C#中的命名空间创建特定的XML文档

The table: parts are not namespaces. table:部分不是名称空间。 They are "namespace prefixes". 它们是“名称空间前缀”。 They are an alias for the actual namespace. 它们是实际命名空间的别名。 They must be declared somewhere. 他们必须在某个地方宣布。 If they are not declared at all in your source XML, then it is not valid XML, and you shouldn't expect to be able to process it. 如果它们的源XML中根本没有声明它们,那么它就不是有效的XML,您不应期望能够处理它。

Are you sure that what you have loaded is the entire XML document? 你确定你加载的是整个XML文档吗? They haven't left off parts to make it simpler? 他们没有放弃零件以使其更简单? Those parts may be the ones that contain the definition of table: . 那些部分可能是包含table:定义的部分table:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM