我有一个查询字符串值。 查询字符串是“a = 1& b = 2& c = 3” 我想把它转换成 < Qurystring> < a> 1 < b> 2< / B个 < c取代; 3'; / C>”中 < /查询字符串>
答案 0 :(得分:0)
详细信息:这是您问题的解决方案。如果它能解决您的问题,请勾选
protected void Page_Load(object sender, EventArgs e)
{
CreateXML();
}
public void CreateXML()
{
// Get query strings
string a = Request.QueryString["a"];
string b = Request.QueryString["b"];
string c = Request.QueryString["c"];
if (a != null && b != null && c != null)
{
string directoryPath = Server.MapPath("~/XMLFiles/");
string fileName = "QueryString.xml";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
//Start writer
XmlTextWriter writer = new XmlTextWriter(directoryPath + fileName, System.Text.Encoding.UTF8);
//Start XM DOcument
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
//ROOT Element
writer.WriteStartElement("Querystring");
//call create nodes method
createNode(a, b, c, writer);
writer.WriteEndElement();
//End XML Document
writer.WriteEndDocument();
//Close writer
writer.Close();
}
}
private void createNode(string a, string b, string c, XmlTextWriter writer)
{
//a
writer.WriteStartElement("a");
writer.WriteString(a);
writer.WriteEndElement();
//b
writer.WriteStartElement("b");
writer.WriteString(b);
writer.WriteEndElement();
//c
writer.WriteStartElement("c");
writer.WriteString(c);
writer.WriteEndElement();
}
答案 1 :(得分:0)
我使用XDocument得到了答案。
XDocument doc = XDocument.Load(Server.MapPath(“xmlFile.xml”));
XElement xmlTree = new XElement(“Parent”);
foreach (String key in Request.Form)
{
xmlTree.Add( new XElement(key, Request.Form[key]));
}
xmlTree.Save("xmlFile.xml");