Hi I am trying to read xml string and trying to replace from old values to new values.. but dont now how to do this… because its read-only. NOTE: I DO NOT WANT TO USE STRING.REPLACE as this may be used in other ways like adding other elements.
string oldValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?><string>Hi This is old values</string>";
string newValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?><string>Hi I am the new values</string>";
Here is what Im trying to do :
private string WriteXmlValue()
{
string currentXml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><string>Hi This is old values</string>";
string newValue = "Hi I am the new values";
string newXmlstring = string.empty;
using (XmlReader xmlReader = XmlReader.Create(new StringReader(currentXml)))
{
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Text:
//TODO Replace xmlReader.Value to newValue??
xmlReader.Value = newValue; //Erroring read only .. How do i modify value??
newXmlstring = xmlReader.value;
break;
}
}
}
return newXmlstring;
}
I am then trying to return this new xmlstring.
To play with the XML data if you are using .net version 3.5 its better to user LINQ to XML.
http://www.codeproject.com/Articles/24376/LINQ-to-XML
or
Manipulate XML data with XPath and XmlDocument (C#)