2012-03-03 101 views
1

我似乎无法弄清楚如何在SilverLight中创建新的XML文件。我尝试使用XMLTextWriter和XMLDocument,但在两个我得到的错误“不存在于当前的上下文中”如何在Silverlight中创建XML文档

我使用System.Xml,我可以使用XMLWriter和XMLReader,但我不能为我的生活弄清楚我做错了什么。请有人指点我正确的方向吗?

谢谢!

回答

4

您正在寻找Linq to Xml和XDocument类。来自MSDN的示例:

XDocument srcTree = new XDocument(
    new XComment("This is a comment"), 
    new XElement("Root", 
     new XElement("Child1", "data1"), 
     new XElement("Child2", "data2"), 
     new XElement("Child3", "data3"), 
     new XElement("Child2", "data4"), 
     new XElement("Info5", "info5"), 
     new XElement("Info6", "info6"), 
     new XElement("Info7", "info7"), 
     new XElement("Info8", "info8") 
    ) 
); 

这是使用System.Xml.Linq命名空间。

您需要参考您的Silverlight项目添加到组装System.Xml.Linq.dll

enter image description here

+0

尝试过的XDocument,SilverLight的不让我使用或添加XML.Linq命名空间。 :( – Blake 2012-03-03 18:14:25

+0

你使用的是什么silverlight版本?你的意思是“它不会让你”? – BrokenGlass 2012-03-03 18:18:38

+0

它应该是可用的:http://msdn.microsoft.com/en-us/library/system.xml。 linq.xdocument%28v = vs.95%29.aspx – 2012-03-03 18:19:23