1

我有一个查询字符串值。查询字符串是“a=1&b=2&c=3”我想把它转换成<Qurystring><a>1<b>2</b><c>3</c>”</Querystring>

4

2 回答 2

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();
}
于 2012-11-24T04:56:05.617 回答
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");
于 2012-11-24T05:27:55.480 回答