添加XML节点和“子节点”

时间:2013-12-02 17:58:25

标签: c# asp.net xml

XML

<Data>
  <products>
    <webs>
      <web>
        <id>1</id>
        <title></title>
        <title_eng></title_eng>
        <image_url></image_url>
        <btns>
          <btn1>
            <id></id>
            <url></url>
          </btn1>
          <btn2>
            <id></id>
            <url></url>
          </btn2>
          <web>
            <webs>
              <products>

我想在<web>下添加所有节点(id,title,title_eng ....)的新整个<webs>。 我成功添加了新内容,但我无法想出如何将with添加到我添加的特定<web>中。

我在C#

中有下一个代码
 protected void add_web_click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml"));
        int foundNode = doc.SelectNodes("Data/products/webs/web").Count;
        foundNode += 1;

        int foundNode_btns = doc.SelectNodes("Data/products/webs/btns").Count;
        foundNode_btns += 1;

        XmlNode addit = doc.SelectSingleNode("Data/products/webs");

        XmlNode xmlRecordNo = doc.CreateNode(XmlNodeType.Element, "web", null);
        XmlElement idelem = doc.CreateElement("id");
        XmlElement title_lem = doc.CreateElement("title");
        XmlElement title_eng_lem = doc.CreateElement("title_eng");
        XmlElement image_url_lem = doc.CreateElement("image_url");
        XmlElement p_lem = doc.CreateElement("p");
        XmlElement p_eng_lem = doc.CreateElement("p_eng");

        addit.AppendChild(xmlRecordNo);
        xmlRecordNo.AppendChild(idelem).InnerText = foundNode.ToString();
        xmlRecordNo.AppendChild(title_lem).InnerText = "הכנס כותרת";
        xmlRecordNo.AppendChild(title_eng_lem).InnerText = "הכנס כותרת באנגלית";
        xmlRecordNo.AppendChild(image_url_lem).InnerText = "";
        xmlRecordNo.AppendChild(p_lem).InnerText = "";
        xmlRecordNo.AppendChild(p_eng_lem).InnerText = "";

        XmlNode datas = doc.SelectSingleNode("Data/products/webs/web/btns");

        XmlNode xmlRecordNo_btns = doc.CreateNode(XmlNodeType.Element, "btn" + foundNode_btns, null);
        XmlElement idelem_btns = doc.CreateElement("id");
        XmlElement image_url_lem_btns = doc.CreateElement("image_url");
        XmlElement url_lem_btns = doc.CreateElement("url");

        datas.AppendChild(xmlRecordNo_btns);
        xmlRecordNo_btns.AppendChild(idelem_btns).InnerText = foundNode_btns.ToString();
        xmlRecordNo_btns.AppendChild(image_url_lem_btns).InnerText = "";
        xmlRecordNo_btns.AppendChild(url_lem_btns).InnerText = "";

        doc.Save(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml"));

        Response.Redirect(Request.RawUrl);
    }

“addit”部分运作良好,我不知道如何继续<btns> <btn> ...添加新的<web>。我用

做了什么
  XmlNode datas = doc.SelectSingleNode("Data/products/webs/web/btns");

不对。

0 个答案:

没有答案