在Windows Phone应用程序中写入Xml
本文关键字:Xml 应用程序 Windows Phone | 更新日期: 2024-10-24 07:31:38
我有这些代码可以很好地为我的WPF应用程序创建xml文档。
var doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
var parentNode = doc.CreateElement("manga");
doc.AppendChild(parentNode);
foreach (var mList in mangaList)
{
var itemNode = doc.CreateElement("item");
var itemAttribute = doc.CreateAttribute("value");
itemAttribute.Value = mList.Key;
itemNode.InnerText = mList.Value;
itemNode.Attributes.Append(itemAttribute);
parentNode.AppendChild(itemNode);
}
var writer = new XmlTextWriter(@"Data'mangalist.xml", null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
现在我想为Windows Phone 7.5创建类似的应用程序,我一直在移植上面的代码以便能够在WP中运行。快速搜索后,我发现XmlDocument在Windows Phone中不可用,必须使用XDocument进行切换。我对XDocument还不熟悉,希望有人能帮助我使我的windows手机应用程序输出相同的xml。感谢
解决方案:
在@Pradeep Kesharwani和@dav_i的良好提示后,我设法移植了上面的代码,使用XDocument和StreamWriter,而不是WP:不可用的XmlDocument和XmlTextWriter
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
var root = new XElement("manga");
var mangaList = new Dictionary<string, string>();
mangaList.Add("conan", "conan");
mangaList.Add("naruto", "naruto");
foreach (var mList in mangaList)
{
var itemNode = new XElement("item");
var itemAttribute = new XAttribute("value", mList.Key);
itemNode.Value = mList.Value;
itemNode.Add(itemAttribute);
root.Add(itemNode);
}
doc.Add(root);
using (var writer = new StreamWriter(@"Data'mangalist2.xml"))
{
writer.Write(doc.ToString());
}
正如我在评论中所说,XDocument
非常直接——
new XDocument(
new XDeclaration("1.0", "utf-8", "no"),
new XElement("root",
new XElement("something",
new XAttribute("attribute", "asdf"),
new XElement("value", 1234),
new XElement("value2", 4567)
),
new XElement("something",
new XAttribute("attribute", "asdf"),
new XElement("value", 1234),
new XElement("value2", 4567)
)
)
)
给出以下
<root>
<something attribute="asdf">
<value>1234</value>
<value2>4567</value2>
</something>
<something attribute="asdf">
<value>1234</value>
<value2>4567</value2>
</something>
</root>
希望这对你有帮助!
要在循环中自动填充,可以执行以下操作:
var somethings = new List<XElement>();
for (int i = 0; i < 3; i++)
somethings.Add(new XElement("something", new XAttribute("attribute", i + 1)));
var document = new XDocument(
new XElement("root",
somethings));
导致
<root>
<something attribute="1" />
<something attribute="2" />
<something attribute="3" />
</root>
此Create方法可用于在wp7 中创建xml文档
private void CreateXml()
{
string xmlStr = "<RootNode></RootNode>";
XDocument document = XDocument.Parse(xmlStr);
XElement ex = new XElement(new XElement("FirstNOde"));
XElement ex1 = new XElement(new XElement("second"));
ex1.Value = "fdfgf";
ex.Add(ex1);
document.Root.Add(new XElement("ChildNode", "World!"));
document.Root.Add(new XElement("ChildNode", "World!"));
document.Root.Add(ex);
string newXmlStr = document.ToString();
}
这将是创建的xml
<RootNode>
<ChildNode>World!</ChildNode>
<ChildNode>World!</ChildNode>
<FirstNOde>
<second>fdfgf</second>
</FirstNOde>
</RootNode>
public void ReadXml()
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("Your xml file name", FileMode.Open);
using (XmlReader reader = XmlReader.Create(isoFileStream))
{
XDocument xml = XDocument.Load(reader);
XElement root1 = xml.Root;
}
}