2012-11-29 51 views
0

我尝试为Visual Studio编写用于XML代码文档的litte插件。 但我的代码,创建XML工作不正常:通过代码构建XML文档

XElement rootElement = new XElement("doc"); 
XElement summaryElement = new XElement("summary"); 

var refElement = new XElement("see"); 
refElement.Add(new XAttribute("cref", codeFunction.Name)); 
summaryElement.Value = string.Format("Initializes a new instance of the {0} class.", seeRefElement); 
rootElement.Add(summaryElement); 
comment = rootElement.ToString(); 

这是输出。

<doc>\r\n <summary>Initializes a new instance of the &lt;see cref="Form1" /&gt; class.</summary>\r\n</doc> 

它应该是:

<doc>\r\n <summary>Initializes a new instance of the <see cref="Form1" />class.</summary>\r\n</doc> 

我应该用另一种方式来创建我的XML任何提示和改进措施的赞赏?

回答

4

替代以下行

summaryElement.Value = string.Format("Initializes a new instance of the {0} class.", refElement); 

summaryElement.Add(new XText("Initializes a new instance of the ")); 
summaryElement.Add(refElement); 
summaryElement.Add(new XText(" class.")); 
+0

谢谢,它的工作原理。你会推荐'XElements',一个'XmlDocument'或'XmlWriter'吗? –

+1

对于您的场景,我认为LINQ to XML是更合适的选择。你也可以看看这个问题http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument,http://stackoverflow.com/questions/1660377/xmldocument-vs-xmlwriter –

+0

谢谢,非常有用..: ) –