2010-11-03 94 views
1

如何将元素添加到wp7中的xml文件中?我发现很多源代码展示了如何在ASP.NET中添加元素,在浏览器上添加Silverlight等等,但是在wp7上没有任何东西。我一直看到我们应该使用XDocument(XML到Linq),只是不知道从哪里开始。谢谢。将元素添加到WP7中的xml文件?

回答

3

WP7上的XDocument用法与silverlight相同。尝试是这样的:

string xmlStr = "<RootNode><ChildNode>Hello</ChildNode></RootNode>"; 
XDocument document = XDocument.Parse(xmlStr); 
document.Root.Add(new XElement("ChildNode", "World!")); 
string newXmlStr = document.ToString(); 
// The value of newXmlStr is now: "<RootNode><ChildNode>Hello</ChildNode><ChildNode>World!</ChildNode></RootNode>" 
+0

是这样的作品,现在我将如何去保存更改到XML文件? – loyalpenguin 2010-11-03 00:37:11

+0

请尝试document.Save(),其中是要保存的文件的流。要在独立存储中获取文件流,请参阅http://msdn.microsoft.com/zh-CN/library/system.io.isolatedstorage.isolatedstoragefilestream%28v=VS.95%29.aspx – 2010-11-03 01:42:09

+0

@ Matt Bridges:使用这个我可以编辑XML谢谢。如果我想创建新的Xml文件...然后需要添加元素里面....想法PLZ – 2011-03-28 05:55:41

1

这是怎么了,我一直为WP7开发:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (var fs = store.OpenFile("MyXmlFile.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
    { 
     var root = new XElement("Root"); 
     var someAttribute = new XAttribute("SomeAttribute", "Some Attribute Value"); 
     var child = new XElement("Child", "Child Value"); 
     var anotherChild = new XElement("AnotherChild", "Another Child Value"); 
     var xDoc = new XDocument(); 
     root.Add(someAttribute, child, anotherChild); 
     xDoc.Add(root); 
     xDoc.Save(fs); 
    } 
}