在 C# 中从列表 <string> 创建 xml 文档
Creating xml document from a list<string> in c#
我有一个字符串列表 "fetchXmlList",其中包含不同的提取 xml。我想使用此列表创建 xml 文档。
Xml 文档看起来像:
<mappings>
<main_fetchxml>
fetchXmlList[0]
</main_fetchxml>
<relatedqueries>
fetchXmlList[1]
fetchXmlList[2]
.
.
.
</relatedqueries>
</mappings>
对于 relatedqueries xml 节点,我将不得不添加 foreach 循环并从第二个列表项开始迭代每个列表项直到列表末尾。
我开始编写以下代码行:
XmlDocument fetchXmlDoc = new XmlDocument();
XmlElement rootNode = fetchXmlDoc.CreateElement("main_fetchxml");
fetchXmlDoc.AppendChild(rootNode);
rootNode.AppendChild(fetchXmlList[0]);
但是不允许将列表项附加到 XmlElement 对象。还有其他办法吗?
如有任何帮助,我们将不胜感激。提前致谢。
1) 你必须有根元素
2) 试试这个代码
public static string Fetch2Xml(string s)
{
return HttpUtility.HtmlDecode(s);
}
private static void Main(string[] args)
{
String s = "<fetch distinct=\"false\" no-lock=\"false\" mapping=\"logical\" />";
String[] sa = new string[] { s, s, s, s, s, s, s };
XmlDocument doc = new XmlDocument();
XmlElement someRoot = doc.CreateElement("mappings");
XmlElement ele = doc.CreateElement("main_fetchxml");
ele.InnerXml = Fetch2Xml(sa[0]);
someRoot.AppendChild(ele);
XmlElement ele2 = doc.CreateElement("relatedqueries");
for (int a = 1; a < sa.Length; ++a)
{
ele2.InnerXml += Fetch2Xml(sa[a]);
}
someRoot.AppendChild(ele2);
doc.AppendChild(someRoot);
doc.Save(@"c:\temp\bla.xml");
}
那也会导致:
<mappings>
<main_fetchxml>
<fetch distinct="false" no-lock="false" mapping="logical" />
</main_fetchxml>
<relatedqueries>
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
</relatedqueries>
</mappings>
你可以使用这个:
public static void SerializeObject(this List<string> list, string fileName)
{
var serializer = new XmlSerializer(typeof(List<string>));
using (var stream = File.OpenWrite(fileName))
{
serializer.Serialize(stream, list);
}
}
public static void Deserialize(this List<string> list, string fileName)
{
var serializer = new XmlSerializer(typeof(List<string>));
using (var stream = File.OpenRead(fileName))
{
var other = (List<string>)(serializer.Deserialize(stream));
list.Clear();
list.AddRange(other);
}
}
我有一个字符串列表 "fetchXmlList",其中包含不同的提取 xml。我想使用此列表创建 xml 文档。
Xml 文档看起来像:
<mappings>
<main_fetchxml>
fetchXmlList[0]
</main_fetchxml>
<relatedqueries>
fetchXmlList[1]
fetchXmlList[2]
.
.
.
</relatedqueries>
</mappings>
对于 relatedqueries xml 节点,我将不得不添加 foreach 循环并从第二个列表项开始迭代每个列表项直到列表末尾。
我开始编写以下代码行:
XmlDocument fetchXmlDoc = new XmlDocument();
XmlElement rootNode = fetchXmlDoc.CreateElement("main_fetchxml");
fetchXmlDoc.AppendChild(rootNode);
rootNode.AppendChild(fetchXmlList[0]);
但是不允许将列表项附加到 XmlElement 对象。还有其他办法吗?
如有任何帮助,我们将不胜感激。提前致谢。
1) 你必须有根元素
2) 试试这个代码
public static string Fetch2Xml(string s)
{
return HttpUtility.HtmlDecode(s);
}
private static void Main(string[] args)
{
String s = "<fetch distinct=\"false\" no-lock=\"false\" mapping=\"logical\" />";
String[] sa = new string[] { s, s, s, s, s, s, s };
XmlDocument doc = new XmlDocument();
XmlElement someRoot = doc.CreateElement("mappings");
XmlElement ele = doc.CreateElement("main_fetchxml");
ele.InnerXml = Fetch2Xml(sa[0]);
someRoot.AppendChild(ele);
XmlElement ele2 = doc.CreateElement("relatedqueries");
for (int a = 1; a < sa.Length; ++a)
{
ele2.InnerXml += Fetch2Xml(sa[a]);
}
someRoot.AppendChild(ele2);
doc.AppendChild(someRoot);
doc.Save(@"c:\temp\bla.xml");
}
那也会导致:
<mappings>
<main_fetchxml>
<fetch distinct="false" no-lock="false" mapping="logical" />
</main_fetchxml>
<relatedqueries>
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
<fetch distinct="false" no-lock="false" mapping="logical" />
</relatedqueries>
</mappings>
你可以使用这个:
public static void SerializeObject(this List<string> list, string fileName)
{
var serializer = new XmlSerializer(typeof(List<string>));
using (var stream = File.OpenWrite(fileName))
{
serializer.Serialize(stream, list);
}
}
public static void Deserialize(this List<string> list, string fileName)
{
var serializer = new XmlSerializer(typeof(List<string>));
using (var stream = File.OpenRead(fileName))
{
var other = (List<string>)(serializer.Deserialize(stream));
list.Clear();
list.AddRange(other);
}
}