如何在C#中运行时创建这种Xml? [英] How Can I Create This Sort Of Xml At Runtime In C#?
本文介绍了如何在C#中运行时创建这种Xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在运行时创建XML,xml看起来像:
Hi,
I want to create a XML at run-time and the xml looks like :
<?xml version=""1.0"" encoding=""utf-8""?>
<root islandLocation=""D:\Regression1\Islands"" otherFileLocation=""D:\Regression1"">
<port>" + portConfigtoBase64String + @"</port>
<setup>" + setuptoBase64String + @"</setup>
<settings>" + settingstoBase64String + @"</settings>
<island>
<name>ABC</name>
<data>" + islandtoBase64String[0] + @"</data>
</island>
<island>
<name>PQR</name>
<data>" + islandtoBase64String[1] + @"</data>
</island>
</root>"
这里< island>的数量标签不固定。每次运行都会有所不同。那么我该如何更改我的代码以便实现这一点,即< island>的数量。标签被控制。
谢谢:)
Here the number of <island> tags is not fixed. It varies at each run. So how can i make changes to my code so that this can be implemented, i.e. the number of <island> tags be controlled.
Thanks :)
推荐答案
这是一个样本
Here is a sample
string[] islandtoBase64String = { "sfsgsdg", "sgtyeyewy", "rhjfjgfjkgfj", "fgjgfjgfjgfj" };
string[] names = { "AAA", "BBB", "CCC","DDD" };
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode productsNode = doc.CreateElement("products");
doc.AppendChild(productsNode);
int cnt = 0;
foreach (string s in names)
{
XmlNode productNode = doc.CreateElement("island");
productsNode.AppendChild(productNode);
XmlNode nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode(s));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("data");
priceNode.AppendChild(doc.CreateTextNode(islandtoBase64String[cnt++]));
productNode.AppendChild(priceNode);
}
doc.Save(@"D:\test.xml");
根据您的要求进行更改
Make changes as per your requirement
使用XmlDocument类,查看本文中的示例
http://forums.asp.net/t/1407620.aspx?Good+Example+of+how+to+create+a+Xml+Document+Programmatically+ 一> [ ^ ]
Use XmlDocument class, see an example in this article
http://forums.asp.net/t/1407620.aspx?Good+Example+of+how+to+create+a+Xml+Document+Programmatically+[^]
public class GenerateXml {
private static void Main() {
string abc="wqfguiqaghekfjakjfa";
string pqr="wgwerhgrewhgerhg";
string xyz="rgwgewrhwgwgwgwegw";
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode productsNode = doc.CreateElement("root");
doc.AppendChild(productsNode);
XmlNode productNode = doc.CreateElement("FileTransfer");
XmlAttribute productAttribute = doc.CreateAttribute("islandLocation");
productAttribute.Value = @"D:\Regression1\Islands";
productNode.Attributes.Append(productAttribute);
XmlAttribute pd = doc.CreateAttribute("otherFilelocation");
pd.Value = @"D:\Regression1";
productNode.Attributes.Append(pd);
productsNode.AppendChild(productNode);
XmlNode nameNode = doc.CreateElement("Port");
nameNode.AppendChild(doc.CreateTextNode(abc));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("setup");
priceNode.AppendChild(doc.CreateTextNode(pqr));
productNode.AppendChild(priceNode);
XmlNode set = doc.CreateElement("settings");
set.AppendChild(doc.CreateTextNode(xyz));
productNode.AppendChild(set);
string[] islandtoBase64String = { "sfsgsdg", "sgtyeyewy", "rhjfjgfjkgfj", "fgjgfjgfjgfj" };
string[] names = { "AAA", "BBB", "CCC","DDD" };
int cnt = 0;
foreach (string s in names)
{
XmlNode pn1 = doc.CreateElement("island");
productsNode.AppendChild(pn1);
XmlNode nn = doc.CreateElement("Name");
nn.AppendChild(doc.CreateTextNode(s));
pn1.AppendChild(nn);
XmlNode nm= doc.CreateElement("data");
nm.AppendChild(doc.CreateTextNode(islandtoBase64String[cnt++]));
pn1.AppendChild(nm);
}
doc.Save(@"D:\test.xml");
doc.Save(Console.Out);
Console.ReadKey();
}
这篇关于如何在C#中运行时创建这种Xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文