首页 新闻 会员 周边

C# XmlDocument问题

0
悬赏园豆:5 [已关闭问题] 关闭于 2013-01-18 16:08

string s = "<a/>";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(s);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
xmlWriter.Indentation = 4;
xmlWriter.Formatting = System.Xml.Formatting.Indented;
doc.WriteContentTo(xmlWriter);
xmlWriter.Close();
string result = Encoding.UTF8.GetString(ms.ToArray());

 

result 结果为<a /> 却不是<a/>

中间多了空格。

 

有没有解决的方法 我用C#不久。呵呵

FiendReam的主页 FiendReam | 初学一级 | 园豆:8
提问于:2012-09-29 11:34
< >
分享
所有回答(3)
0

对于自闭型tag的一种格式规范而已。

如果有特殊要求可以处理一下嘛

Ethan轻叹 | 园豆:996 (小虾三级) | 2012-09-29 11:38

这个 有没有 代码 呵呵 

支持(0) 反对(0) FiendReam | 园豆:8 (初学一级) | 2012-09-29 13:40
0

现在多了个空格对你的程序有影响吗?这也许是内部的固定格式上的规范,具体的我也不清楚。还有你使用的是XmlDocument,而这个操作XML起来挺麻烦的,代码量会多一点,如果你的框架允许,建议你使用LINQ TO XML,这个比较简单操控XML节点,只要少量的代码就能完整对节点的增删改查功能,非常方便。这里有个网站有相关的例子,一看就会。http://www.dobug.net/showforum-11.html

pasig10038 | 园豆:387 (菜鸟二级) | 2012-10-02 11:16
0

// 去掉空标签的空格问题<a />
int iIndex_3 = strFormat.IndexOf(" />");
while(iIndex_3 != -1)
{
string strPrefix = strFormat.Substring(0, iIndex_3);
string strSuffix = strFormat.Substring(iIndex_3).Trim();

strFormat = strPrefix + strSuffix;
iIndex_3 = strFormat.IndexOf(" />");
}

FiendReam | 园豆:8 (初学一级) | 2013-01-18 16:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册