Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a string containing XML(string stringXML), I load it in an XML document and make changes in that XML document dynamically,

C#
XmlDocument doc = new XmlDocument();
            doc.LoadXml(stringXML);
            var fnode = doc.GetElementsByTagName("feature")[0];
            var fval = fnode.Attributes["name"].Value;
            if (fval == "IMAGING_SERVICES")
            {
                var servcnode = doc.GetElementsByTagName("service")[0];
                var namenode = servcnode.Attributes["name"].Value;
                if (namenode == "PRINTING")
                {
                    doc.InnerXml.Replace(namenode, "IMAGING_OPTIONS");
                }
            }



Then i need to send stringXML to the server with changes in it, the problem arising is that it is not changing value of doc.
Second after changing in doc how can i also make changes in stringXML?
Posted
Updated 17-Apr-11 22:00pm
v2

After changing your XML, doc.OuterXml will have the updated the String.

C#
XmlDocument doc = new XmlDocument();
            doc.LoadXml(stringXML);
            var fnode = doc.GetElementsByTagName("feature")[0];
            var fval = fnode.Attributes["name"].Value;
            if (fval == "IMAGING_SERVICES")
            {
                var servcnode = doc.GetElementsByTagName("service")[0];
                var namenode = servcnode.Attributes["name"].Value;
                if (namenode == "PRINTING")
                {
                    doc.InnerXml.Replace(namenode, "IMAGING_OPTIONS");
                }
            }

String newXmlString = doc.OuterXml;


The newXmlString will have the updated XML.

Do you want to save the doc too?
Then you can reload the doc with the new XmlString :
doc.LoadXml(newXmlString);
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 18-Apr-11 4:54am    
That should be it! 5+
Tarun.K.S 18-Apr-11 4:58am    
Thanks! :)
Yes that should do it!
Espen Harlinn 18-Apr-11 17:55pm    
My 5+
Tarun.K.S 19-Apr-11 1:56am    
Thanks!
I don't event understand the problem: every node and attribute in DOM you're using is modifiable. Just look at help on related classes.

—SA
 
Share this answer
 
Everything is working as expected:
If you change stringXML after the document has been constructed from the string you would have to repeat doc.LoadXml(stringXML).
If you make changes to doc and want them manifested in stringXML you'd have to render doc to a string again.

Best Regards,

-MRB
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900