Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#更新存储在XML中的数据?_C#_Asp.net_Xml - Fatal编程技术网

如何使用C#更新存储在XML中的数据?

如何使用C#更新存储在XML中的数据?,c#,asp.net,xml,C#,Asp.net,Xml,我正在使用以下程序来更新使用C#存储在xml文档中的数据 我有两个名为username和password的字段。如果我尝试插入数据,它将成功添加 我的问题是当我试图更新已经存储在xml文档中的数据时。我无法更新记录。那么如何使用C#更新XLM文档中的记录呢 我有以下异常NullReferenceException,它起源于此行: root.ReplaceChild(newCd, oldCd); 按钮1\u单击添加数据,按钮2\u单击更新数据 using System; using System

我正在使用以下程序来更新使用C#存储在xml文档中的数据

我有两个名为
username
password
的字段。如果我尝试插入数据,它将成功添加

我的问题是当我试图更新已经存储在xml文档中的数据时。我无法更新记录。那么如何使用C#更新XLM文档中的记录呢

我有以下异常
NullReferenceException
,它起源于此行:

root.ReplaceChild(newCd, oldCd);
按钮1\u单击
添加数据,
按钮2\u单击
更新数据

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Connection();      
    }

    protected void Connection()
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(@"D:\vijay.net\xmlstorage\XMLFile.xml");
        XmlNode xmlnod = xmldoc.SelectSingleNode("records");
        XmlNode xmlrec = xmlnod.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "record", ""));
        xmlrec.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "Username", "")).InnerText = TextBox1.Text;
        xmlrec.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "password", "")).InnerText = TextBox2.Text;
        xmldoc.Save(@"D:\vijay.net\xmlstorage\XMLFile.xml");
        Response.Write("Successfully saved in xml file");
        TextBox1.Text = "";
        TextBox2.Text = "";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {    
        XmlTextReader reader = new XmlTextReader(@"D:\vijay.net\xmlstorage\XMLFile.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        reader.Close();

        XmlNode oldCd;
        XmlElement root = doc.DocumentElement;
        oldCd = root.SelectSingleNode("/catalog/cd[Username='" + TextBox1.Text + "']");
        XmlElement newCd = doc.CreateElement("cd");
        newCd.SetAttribute("Password", TextBox2.Text);

        newCd.InnerXml = "<Username>" + this.TextBox1.Text + "</Username>";
        root.ReplaceChild(newCd, oldCd);
         doc.Save(@"D:\vijay.net\xmlstorage\XMLFile.xml");
    }
}
使用系统;
使用系统配置;
使用系统数据;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Xml.Linq;
使用System.Xml;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
连接();
}
受保护的无效连接()
{
XmlDocument xmldoc=新的XmlDocument();
Load(@“D:\vijay.net\xmlstorage\XMLFile.xml”);
XmlNode XmlNode=xmldoc.SelectSingleNode(“记录”);
XmlNode xmlrec=XmlNode.AppendChild(xmldoc.CreateNode(XmlNodeType.Element,“record”和“”);
AppendChild(xmldoc.CreateNode(XmlNodeType.Element,“Username”,“”)InnerText=TextBox1.Text;
AppendChild(xmldoc.CreateNode(XmlNodeType.Element,“password”和“”).InnerText=TextBox2.Text;
xmldoc.Save(@“D:\vijay.net\xmlstorage\XMLFile.xml”);
Write(“成功保存在xml文件中”);
TextBox1.Text=“”;
TextBox2.Text=“”;
}
受保护的无效按钮2\u单击(对象发送者,事件参数e)
{    
XmlTextReader=新的XmlTextReader(@“D:\vijay.net\xmlstorage\XMLFile.xml”);
XmlDocument doc=新的XmlDocument();
文件加载(读卡器);
reader.Close();
xmlnodeoldcd;
XmlElement根=doc.DocumentElement;
oldCd=root.SelectSingleNode(“/catalog/cd[Username=”+TextBox1.Text+“]”);
XmlElement newCd=doc.CreateElement(“cd”);
newCd.SetAttribute(“密码”,TextBox2.Text);
newCd.InnerXml=“”+this.TextBox1.Text+”;
root.ReplaceChild(newCd、oldCd);
doc.Save(@“D:\vijay.net\xmlstorage\XMLFile.xml”);
}
}

问题似乎是XML文档和用于选择要修改的节点的XPath表达式之间的不兼容

根据
Connection()
方法中的代码,生成的XML结构如下所示:

<records>
    <record>
        <username>SomeUsername</username>
        <password>SomePassword</password>
    </record>
</records>
它返回
null
,因为它不反映实际的XML结构。您需要更改XPath表达式以根据其内容选择
oldCd
节点:

oldCd = root.SelectSingleNode("//username[contains(., '" + TextBox2.Text + "')]");

你看过XmlDataSource控件了吗,它为你做了所有的工作。。。oldCd=root.SelectSingleNode(//username[包含(,“”+TextBox2.Text+“)]”);如果我这样使用,我也会面临同样的错误。你能发布正在加载的实际XML吗?
oldCd = root.SelectSingleNode("//username[contains(., '" + TextBox2.Text + "')]");