在 C#.net 中将文本框值写入 xml 文件

发布于 2024-12-27 04:48:17 字数 1019 浏览 1 评论 0原文

我有两个文本框,即 txtUserid 和 txtPassowrd 。

我正在将文本框中输入的值写入 XML 文件,但我不希望相同的 txtuserid 值在 XML 中写入两次 -它应该被覆盖。

例如:

  • 如果我输入 txtUserid=2txtPassword=I
  • ,第二次输入 txtUserid=2 code> 和 txtPassword=m

那么我只想在 XML 文件中保留一个条目:

对于上面的示例: txtUserid=2textPassword=m

代码:

XDocument Xdoc = new XDocument(new XElement("Users"));
if (System.IO.File.Exists("D:\\Users.xml"))
{
   Xdoc = XDocument.Load("D:\\Users.xml");
}
else
{
   Xdoc = new XDocument();
}

XElement xml = new XElement("Users",
               new XElement("User",
               new XAttribute("UserId", txtUserName.Text),
               new XAttribute("Password", txtPassword.Text)));

if (Xdoc.Descendants().Count() > 0)
{
   Xdoc.Descendants().First().Add(xml);
}
else
{
   Xdoc.Add(xml);
}

Xdoc.Save("D:\\Users.xml");

I have two TextBoxes namely txtUserid and txtPassowrd.

I'm writing the values entered in textboxes to a XML file but I do not want the same txtuserid values to be written twice in XML - it should be overwritten.

For example :

  • If I enter in txtUserid=2 and txtPassword=I
  • and the second time if I enter txtUserid=2 and txtPassword=m

then I only want one entry to be kept in the XML file :

For the above example: the txtUserid=2 and textPassword=m

The code:

XDocument Xdoc = new XDocument(new XElement("Users"));
if (System.IO.File.Exists("D:\\Users.xml"))
{
   Xdoc = XDocument.Load("D:\\Users.xml");
}
else
{
   Xdoc = new XDocument();
}

XElement xml = new XElement("Users",
               new XElement("User",
               new XAttribute("UserId", txtUserName.Text),
               new XAttribute("Password", txtPassword.Text)));

if (Xdoc.Descendants().Count() > 0)
{
   Xdoc.Descendants().First().Add(xml);
}
else
{
   Xdoc.Add(xml);
}

Xdoc.Save("D:\\Users.xml");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2025-01-03 04:48:17

在现有 XML 文档中搜索 UserId 属性与当前节点匹配的节点,如果匹配,则修改该节点,否则创建一个新节点。

我想您的代码将类似于以下内容:

        List<XElement> list = Xdoc.Descendants("User").Where(el => el.Attribute("UserId").Value == txtUserName.Text).ToList();
        if (list.Count == 0)
        {
            // Add new node
        }
        else
        {
            // Modify the existing node
        }

编辑:为了响应您的评论,编辑 XElement 的代码将类似于

string myValue = "myValue";
list.First().Attribute("ElementName").SetValue(myValue);

Search your existing XML document for a node where the UserId attribute matches your current one, and if it does, modify that one, else make a new one.

I'd imagine that your coude would resemble the following:

        List<XElement> list = Xdoc.Descendants("User").Where(el => el.Attribute("UserId").Value == txtUserName.Text).ToList();
        if (list.Count == 0)
        {
            // Add new node
        }
        else
        {
            // Modify the existing node
        }

Edit: In response to your comment, the code to edit your XElement would look something like

string myValue = "myValue";
list.First().Attribute("ElementName").SetValue(myValue);
猫七 2025-01-03 04:48:17

在 C# 中将文本框值写入 XML 文件

protected void btnSave_Click(object sender, EventArgs e)
{   
    // Open the XML doc  
    System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
    myXmlDocument.Load(Server.MapPath("InsertData.xml"));
    System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;

    // Create new XML element and populate its attributes  
    System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("entry");
    myXmlElement.SetAttribute("Userid", Server.HtmlEncode(textUserid.Text));
    myXmlElement.SetAttribute("Username", Server.HtmlEncode(textUsername.Text));
    myXmlElement.SetAttribute("AccountNo", Server.HtmlEncode(txtAccountNo.Text));
    myXmlElement.SetAttribute("BillAmount", Server.HtmlEncode(txtBillAmount.Text));


    // Insert data into the XML doc and save  
    myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
    myXmlDocument.Save(Server.MapPath("InsertData.xml"));

    // Re-bind data since the doc has been added to  
    BindData();


    Response.Write(@"<script language='javascript'>alert('Record inserted Successfully Inside the XML File....')</script>");
    textUserid.Text = "";
    textUsername.Text = "";
    txtAccountNo.Text = "";
    txtBillAmount.Text = "";  
}

void BindData()
{
    XmlTextReader myXmlReader = new XmlTextReader(Server.MapPath("InsertData.xml"));
    myXmlReader.Close();
}  

Writing textbox values to XML file in C#

protected void btnSave_Click(object sender, EventArgs e)
{   
    // Open the XML doc  
    System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
    myXmlDocument.Load(Server.MapPath("InsertData.xml"));
    System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;

    // Create new XML element and populate its attributes  
    System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("entry");
    myXmlElement.SetAttribute("Userid", Server.HtmlEncode(textUserid.Text));
    myXmlElement.SetAttribute("Username", Server.HtmlEncode(textUsername.Text));
    myXmlElement.SetAttribute("AccountNo", Server.HtmlEncode(txtAccountNo.Text));
    myXmlElement.SetAttribute("BillAmount", Server.HtmlEncode(txtBillAmount.Text));


    // Insert data into the XML doc and save  
    myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
    myXmlDocument.Save(Server.MapPath("InsertData.xml"));

    // Re-bind data since the doc has been added to  
    BindData();


    Response.Write(@"<script language='javascript'>alert('Record inserted Successfully Inside the XML File....')</script>");
    textUserid.Text = "";
    textUsername.Text = "";
    txtAccountNo.Text = "";
    txtBillAmount.Text = "";  
}

void BindData()
{
    XmlTextReader myXmlReader = new XmlTextReader(Server.MapPath("InsertData.xml"));
    myXmlReader.Close();
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文