C# .Net 中 XmlElement 中的空白 [英] White space in XmlElement in C# .Net

查看:22
本文介绍了C# .Net 中 XmlElement 中的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XmlElement child = doc.CreateElement(element);

其中docXmlDocument 的对象.当代码使用 element=Tom and Jerry 执行上述行时,我收到以下错误:

Where doc is an object of XmlDocument. When the code executes the above line with element=Tom and Jerry, I get the following error:

' ' 字符,十六进制值 0x20,不能包含在名称中.

我应该怎么做才能在 XmlDocument 中包含 ' '?我不能用其他任何东西代替它.

What should I do to include ' ' in XmlDocument? I can not replace it with anything else.

XML 元素不支持 name 的其他字符是什么?

What are the other characters which XML element does not support for name ?

推荐答案

我想您想要一个具有 valueTom and Jerry"的元素,这很好.

I suppose you want an element with the value "Tom and Jerry", which is fine.

元素或属性的名称中不能有空格,这是 XML 语法的一部分.

It is part of the XML syntax that you cannot have a space in the name of an element or attribute.

一种可能的方法:

XmlElement child = doc.CreateElement("cartoon");
child.InnerText = "Tom and Jerry";

产生

<cartoon>Tom and Jerry</cartoon>

另外,如果可以,请考虑 XDocument.比 XmlDocument 简单多了.

Aside, consider XDocument when you can. Much easier than XmlDocument.

XElement child = new XElement("cartoon", "Tom and Jerry");

这篇关于C# .Net 中 XmlElement 中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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