每次提交信息时都将文本信息添加到不同的XML元素c#

时间:2016-06-15 02:53:41

标签: c# xml

我有一个XML文件,它使用Button_Click收集信息,因此它从空开始。

XML示例

<marina>
<dockone>
</dockone>
<docktwo>
</docktwo>
</marina>

当我从文本框提交信息时,会创建一个名为 slipone 的新XmlNode,另一个名为 reg 的XmlNode嵌套在该文件框中。

XML示例2

<marina>
<dockone>
<slipone>
<reg>12345</reg>
<slipone>
</dockone>
<docktwo>

</docktwo>
</marina>

我试图创建一个if / else语句,它将添加一个名为 sliptwo 的新XmlNode, reg 仍然嵌套在其中,如果 slipone 已经有文字,如下:

<marina>
<dockone>
<slipone>
<reg>12345</reg>
<slipone>
<sliptwo>
<reg>67890</reg>
<sliptwo>
</dockone>
<docktwo>
</docktwo>
</marina>

然而,我得到的最接近的是另一个XMlnode仍然被创建,但是它将自己标记为 slipone ,我不确定我做错了什么:

 <marina>
<dockone>
<slipone>
<reg>12345</reg>
<slipone>
<slipone>
<reg>67890</reg>
<slipone>
</dockone>
<docktwo>
</docktwo>
</marina>

这是我一直在玩的一个例子。忽略了操作员,因为我已经使用了反复试验,但仍然无处可去。请帮忙!

C#示例

XmlDocument XmlDocObj1 = new XmlDocument();
        XmlDocObj1.Load(Server.MapPath("~/App_Data/SlipData.xml"));
        XmlNode rootnode1 = XmlDocObj1.SelectSingleNode("marina/dockone");
        XmlNode dockone = rootnode1.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "slipone", ""));
        XmlNode docktwo = rootnode1.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "sliptwo", ""));
        XmlNode dockthree = rootnode1.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "slipthree", ""));




            if (regfinal.Text != dockone.InnerText)
            {

                dockone.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "Reg", "")).InnerText = regfinal.Text;
                XmlDocObj1.Save(Server.MapPath("/App_Data/SlipData.xml"));
            }

        else if (regfinal.Text == dockone.InnerText)
            {

                docktwo.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "Reg", "")).InnerText = regfinal.Text;
                XmlDocObj1.Save(Server.MapPath("/App_Data/SlipData.xml"));
            }

1 个答案:

答案 0 :(得分:0)

你的逻辑不会按照我的想法去做,因为唯一的时间(regfinal.Text!= dockone.InnerText)评估为false就是你在文本控件中什么也没输入。

我相信你可能想说是否存在dockone然后创建另一个名为docktwo的节点。这将要求您改变逻辑。

一些非常简单的代码可以让你走得更远。不是为了完美或解决所有问题......

private void button1_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDocObj1 = new XmlDocument();
        XmlDocObj1.Load(AppDomain.CurrentDomain.BaseDirectory.ToString()+"test.xml");
        XmlNode rootnode1 = XmlDocObj1.SelectSingleNode("marina/dockone");
        XmlNode dockone = rootnode1.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "slipone", ""));
        XmlNode docktwo = rootnode1.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "sliptwo", ""));
        XmlNode dockthree = rootnode1.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "slipthree", ""));

        //jsh: old logic
        //if (textBox1.Text != dockone.InnerText)

        //new logic to test whether we have already created the dockone node which should only occur once
        //you already have the logic for selecting the dockone node above...now just test if you already have it.
        //NOTE: you may actually want a switch statement given that you avhe dockone, docktwo, and dockthree or at least another 
        //      if statement to see if docktwo has been created and thus creaste dockthree.
        if (rootnode1 == null )
        {
            dockone.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "Reg", "")).InnerText = textBox1.Text;
            XmlDocObj1.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + "test.xml");
        }
        //else if (textBox1.Text == dockone.InnerText) jsh: old logic
        else
        {
            docktwo.AppendChild(XmlDocObj1.CreateNode(XmlNodeType.Element, "Reg", "")).InnerText = textBox1.Text;
            XmlDocObj1.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + "test.xml");
        }
    }