Update xml file with xmltextReader in c#

596 views Asked by At

I have xml file like this test.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;
          }

What I have to do to following thing:

  1. I have to save updated value in xml file with xmltestReader and xmldocument
  2. I'm getting request from third client about messageID so I have to check it's exist in xml file or not (e.g get request for msg1 so I have to match it xml file it's there or not).

hope I'm clear with my question.

1

There are 1 answers

0
Santhosh Kumar On

//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>