How to update an XML string
I am wondering how do you go about updating an XML string using XML document...
I will be changing the values of attributes in an xmlDocument (this will be a large string)... Looking online I was a little confused about the proper way to do this... Here is a sample of what I am trying to do...
//get the value at the key from the form collection
var value = formCollection[key.ToString()];
//lookup the specifc <node> in the xml file and match it to the key... should be equal to the path's array found in _EditTemplate values above...
XmlNode xElm = xmlTemplate.SelectSingleNode(string.Format("//dataTemplateSpecification/templates/template/elements/element[@name=\"{0}\"]", key.ToString()));
//get the value attribute va
XmlAttribute tempAtt = xElm.Attributes["value"];// = formCollection[key.ToString()];
//Attempt to set the attribute value to the correct value
tempAtt.Valu开发者_Python百科e = value;// formCollection[key.ToString()];
Does that seem like a rational way to update an xml String? Remember, I don't want to save this to a file. I want to update the string and go from there I need to do more processing...
If I just wanted to set an attribute value (without looking at the previous value), I'd use XmlElement.SetAttribute
.
(As an aside, if you're using .NET 3.5 or higher, I would definitely look at LINQ to XML instead of the original DOM API.)
精彩评论