C# 在Windows Phone应用程序中编写Xml
我有这段代码,可以很好地为我的WPF应用程序创建xml文档C# 在Windows Phone应用程序中编写Xml,c#,xml,windows-phone-7,C#,Xml,Windows Phone 7,我有这段代码,可以很好地为我的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 m
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();
现在,我想为WindowsPhone7.5创建类似的应用程序,我一直在移植上面的代码,以便能够在WP中运行。快速搜索后,我发现XmlDocument在Windows Phone中不可用,必须使用XDocument切换。我对XDocument还不熟悉,希望有人能帮助我使我的windows phone应用程序输出相同的xml。
谢谢
解决方案:
在@Pradeep Kesharwani和@dav_i给出良好提示后,我成功地移植了上述代码,以使用XDocument和StreamWriter,而不是XmlDocument和XmlTextWriter,这两种代码不适用于WP:
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>
正如我在评论中所说,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>
此创建方法可用于在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();
}
此创建方法可用于在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();
}
XDocument并不难,事实上,它比XmlDocument更容易。你为什么不在放弃之前玩一玩呢?@dav_i你这么说没关系。但是关于XDocument中AppenChild、CreateElement和CreateAttribute的对应方法有什么提示吗?或者XDocument有不同的方法,所以我需要以不同的方式使用它吗?事实上,XDocument并不难,它比XmlDocument更简单。你为什么不在放弃之前玩一玩呢?@dav_i你这么说没关系。但是关于XDocument中AppenChild、CreateElement和CreateAttribute的对应方法有什么提示吗?或者XDocument有不同的方法,所以我需要以不同的方式使用它吗?谢谢你的代码,但是你能在你的回答plz中包含该代码预期的xml输出吗?xml输出将让我更好地了解你的代码示例实际在做什么,这样我就可以调整它以满足我的需要。再次感谢Hanks提供的代码,但是您能在您的回答plz中包含该代码的预期xml输出吗?xml输出将让我更好地了解您的代码示例实际在做什么,以便我可以调整它以满足我的需要。再次感谢汉克斯的提示,非常感谢。你们知道我是否需要根据列表变量中的项数添加不同数量的元素吗?谢谢你们的提示,非常感谢。如果我需要根据列表变量中的项数添加不同数量的元素,你知道吗?