Skip to content

Instantly share code, notes, and snippets.

@jdowd7
Last active August 29, 2015 14:01
Show Gist options
  • Save jdowd7/a5771508d9f7698cfc68 to your computer and use it in GitHub Desktop.
Save jdowd7/a5771508d9f7698cfc68 to your computer and use it in GitHub Desktop.
change values within a xml node
//parse the XML, might as well use Xpath to avoid dupe root nodes.... ughhhhh
System.Xml.XPath.XPathDocument xmlResults = new System.Xml.XPath.XPathDocument(customObjectThatHasXmlData.Data.InnerXml.ToString());
System.Xml.XPath.XPathNavigator xmlResultsNav = xmlResults.CreateNavigator();
System.Xml.XPath.XPathExpression xmlResultsExpression;
xmlResultsExpression = xmlResultsNav.Compile(@"/Data/result xmlns=""/time_stamp"); //in this case was trying to get the Tstamp
System.Xml.XPath.XPathNodeIterator xmlResultsNavIterator = xmlResultsNav.Select(xmlResultsExpression);
while(xmlResultsNavIterator.MoveNext())
{
if (xmlResultsNavIterator.Current.Name.Equals("time_stamp"))
{
DateTime newDT = RoundUp(DateTime.Parse(xmlResultsNavIterator.Current.Value), TimeSpan.FromMinutes(5));
xmlResultsNavIterator.Current.SetValue(newDT.ToString());
}
}
//source: http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C
@jdowd7
Copy link
Author

jdowd7 commented Jun 10, 2014

foreach (System.Xml.XmlNode node in mbsSearchResultData.Data)
{

                foreach (System.Xml.XmlNode singleChildNode in node.ChildNodes)
                {
                    if(singleChildNode.Name.Equals("time_stamp"))
                    {
                        DateTime newDT = RoundUp(DateTime.Parse(singleChildNode.InnerText), TimeSpan.FromMinutes(5));
                        //newDT = newDT.ToUniversalTime();       //.ToLocalTime();
                        singleChildNode.InnerText = newDT.ToString();
                        singleChildNode.InnerXml = newDT.ToString();
                    }

                }         

            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment