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

时间:2014-11-21 15:49:30

标签: c# xml xmltextreader

我有像这个test.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
  2. 在xml文件中保存更新的值
  3. 我从第三个客户端收到有关messageID的请求,所以我必须检查它是否存在于xml文件中(例如获取msg1的请求,所以我必须将它与xml文件相匹配)&#39; s是否有)。
  4. 希望我清楚自己的问题。

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
//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
&#13;
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> 
&#13;
&#13;
&#13;