Parse and replace element in an xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gtk
    New Member
    • Jan 2007
    • 4

    #1

    Parse and replace element in an xml file

    Hello,
    i have an xml file, and what i want to do is to replace the value of a specific element (<umps>) with some text dynamically.
    More specifically, here is the xml life:

    Code:
    <rule id = "onlyFirstName">
    		<tag>FirstName=''; userID=''</tag>
    		<one-of>
    			<item><umps>PersonalDetails:FirstName</umps><tag>FirstName=<umps>PersonalDetails:FirstName</umps>; userID=<umps>userID</umps></tag></item>
    		</one-of>
    </rule>
    and here is what i want to do:

    Code:
    <rule id = "onlyFirstName">
    		<tag>FirstName=''; userID=''</tag>
    		<one-of>
    			<item><umps>new_Data</umps><tag>FirstName=<umps>another_new_Data_dynamically</umps>; userID=<umps>another_new_one</umps></tag></item>
    		</one-of>
    </rule>
    I'm trying to replace the content of the "umps" element with another text. I mean, i don't care about the current content of the umps element.

    I tried to use Regular expressions , but i found difficulty in managing it.

    Any help??
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Are you trying to do this by parsing the string or by using the DOM model?

    using the DOM model will be a lot easier.

    The XmlDocument Class is what you would need for this.

    If you face issues with searching for examples using the XmlDocument do get back.

    Comment

    • gtk
      New Member
      • Jan 2007
      • 4

      #3
      By parsing the string.
      Now, i'm trying to do it with Xml classes like XmlTextReader, XmlNodeType, but i have been confused.

      Which is the best way to edit xml node values dynamically? i mean, i don't want to search for the substring "<umps>Personal Details:FirstNa me</umps>" but each <umps>...</umps>

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        hi
        i have not yet used the XMLReader....i think thats based on the SAX model

        i have written a code snippet which works for your xml type.

        [CODE=cpp]System.Xml.XmlD ocument doc = new System.Xml.XmlD ocument();
        doc.LoadXml("<r ule id = 'onlyFirstName' ><tag>FirstName =''; userID=''</tag><one-of><item><umps> PersonalDetails :FirstName</umps><tag>First Name=<umps>Pers onalDetails:Fir stName</umps>; userID=<umps>us erID</umps></tag></item></one-of></rule>");

        System.Xml.XmlN ode ruleNode = doc.GetElements ByTagName("rule ")[0];
        System.Xml.XmlN ode umpsNode = ruleNode.Select SingleNode("one-of/item/umps");

        MessageBox.Show (umpsNode.Inner Text);
        umpsNode.InnerT ext = "new value";
        MessageBox.Show (umpsNode.Inner Text);
        MessageBox.Show (doc.InnerXml);[/CODE]

        Please note that when loading you might have to use the Load Method instead of the LoadXML ifyou are loading the xml from a streat / file !

        Comment

        • gtk
          New Member
          • Jan 2007
          • 4

          #5
          thank you for your help.
          One more question: if i dont know anything about the exact position of the <umps> tag ( e.g.we don't know the location "one-of/item/umps" as we previously did) in the xml file, is it possible to accomplish the same task ?

          Thanks in advance

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            yes, you get all the nodes of umps tags
            or all nodes whose parent node is one-of

            thats where expath comes in
            the code whcich says tag/one-of/umps is xpath

            take some tutorails on xpath and it will get clearer

            Comment

            Working...