在 c# 中使用 xmltextReader 更新 xml 文件

本文关键字:更新 xml 文件 xmltextReader | 更新日期: 2023-09-27 18:31:29

我有像这个测试这样的xml文件.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Message>
<Item Name ="msg1" Status ="false"/>
<Item Name="msg2" Status="false"/>
</Message> 

        System.Xml.XmlTextReader textReader = new System.Xml.XmlTextReader(test.xml);
        System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
        xdoc.Load(test.xml);
        var testreader = xdoc.DocumentElement.ChildNodes;
        string name = string.Empty;
        string value = string.Empty;

        if (message.MsgType == 10005)
        {
            value = "true";
        }
        else if (message.MsgType == 10002)
        {
            value = "false";
        }
         foreach (var mychild in testreader)
         {
             var childatt = ((System.Xml.XmlElement)mychild);
             name = childatt.Attributes["Name"].Value;
             value = childatt.Attributes["Status"].Value;
          }

我必须对以下事情做什么:

  1. 我必须使用 xmltestReader 和 xmldocument 将更新的值保存在 xml 文件中
  2. 我收到来自第三个客户端的关于 messageID 的请求,所以我必须检查它是否存在于 xml 文件中(例如,获取 msg1 的请求,所以我必须匹配它 xml 文件是否存在)。

希望我的问题很清楚。

在 c# 中使用 xmltextReader 更新 xml 文件

//Get Message 
//txtsearch text u have to pass message Id
           var xmlFilePath = Server.MapPath("Test.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFilePath);
       
            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/TableMessage/Message");
            string msgID = "",msgname="";
            foreach (XmlNode node in nodeList)
            {
                if (node.SelectSingleNode("Message_Details").InnerText == "True" && node.SelectSingleNode("Message_id").InnerText==txtsearchtext.Text)
                {
                    msgID = node.SelectSingleNode("Message_id").InnerText;
                    msgname = node.SelectSingleNode("Message_name").InnerText;
                 
                }
               
                               
            }
//store Updated Xml in ur project  xml
//get the information from end user and pass the value to correspoding fields
           XmlDocument xmlEmloyeeDoc = new XmlDocument();
            xmlEmloyeeDoc.Load(Server.MapPath("~/Test.xml"));
            XmlElement ParentElement = xmlEmloyeeDoc.CreateElement("Message");
            XmlElement ID = xmlEmloyeeDoc.CreateElement("Message_id");
            ID.InnerText = "6";
            XmlElement message = xmlEmloyeeDoc.CreateElement("Message_name");
            message.InnerText = "Message6";
            XmlElement Details = xmlEmloyeeDoc.CreateElement("Message_Details");
            Details.InnerText = "True";
            ParentElement.AppendChild(ID);
            ParentElement.AppendChild(message);
            ParentElement.AppendChild(Details);     
            xmlEmloyeeDoc.DocumentElement.AppendChild(ParentElement);
            xmlEmloyeeDoc.Save(Server.MapPath("~/Test.xml"));
I hope that code will help u
you will customize ur xml file like this
<?xml version="1.0" encoding="utf-8"?>
<TableMessage>
  <Message>
    <Message_id>Message1</Message_id>
    <Message_name>Message1</Message_name>
    <Message_Details>True</Message_Details>
  </Message>
  <Message>
    <Message_id>Message2</Message_id>
    <Message_name>Message2</Message_name>
    <Message_Details>True</Message_Details>
  </Message>
  <Message>
    <Message_id>Message3</Message_id>
    <Message_name>Message3</Message_name>
    <Message_Details>False</Message_Details>
  </Message>
</TableMessage>