I have a c# app that writes to XML using the following code:
//Write last compliant elements to state XML if allCompliant bool == true
if (allCompliant == true)
{
if (File.Exists(MainEntry.thirdPartyStateXMLPath))
{
XDocument doc = XDocument.Load(MainEntry.thirdPartyStateXMLPath);
DateTime localCurrentTime = DateTime.Now;
DateTime utcCurrentTime = DateTime.UtcNow;
XElement root = new XElement("Compliance_Status");
root.Add(new XElement("Last_Known_Compliant_UTC", utcCurrentTime));
root.Add(new XElement("Last_Known_Compliant_LocalTime", localCurrentTime.ToString()));
doc.Element("Compliance_Items").Add(root);
doc.Save(MainEntry.thirdPartyStateXMLPath);
}
}
That renders the following XML:
<?xml version="1.0" encoding="utf-8"?>
<Compliance_Items>
<Compliance_Status>
<Last_Known_Compliant_UTC>2014-04-03T23:22:31.507088Z</Last_Known_Compliant_UTC>
<Last_Known_Compliant_LocalTime>4/3/2014 4:22:31 PM</Last_Known_Compliant_LocalTime>
</Compliance_Status>
</Compliance_Items>
This code generates the *Last_Known_Compliant_UTC* and *Last_Known_Compliant_LocalTime* elements and values. On subsequent runs of the code I want it to only replace the values of the existing elements, but as written now the following is re-created each time and keeps stacking in the XML:
<Compliance_Status>
<Last_Known_Compliant_UTC>2014-04-03T23:22:31.507088Z</Last_Known_Compliant_UTC>
<Last_Known_Compliant_LocalTime>4/3/2014 4:22:31 PM</Last_Known_Compliant_LocalTime>
</Compliance_Status>
How can I achieve the desired effect?
If you want to get a specific item and modify it you can do the following:
var xmlDocument = XDocument.Load("path");
var element = xmlDocument
.Descendants("Compliance_Status")
.FirstOrDefault(x => (DateTime)x.Element("Last_Known_Compliant_UTC") == someValue)
if(element != null)
/* update the value simply using element.Value = something
and save the xml file xmlDocument.Save("path") */
If you want to replace your element with another element you can use XElement.ReplaceWith
method.
I have a c# app that writes to XML using the following code:
//Write last compliant elements to state XML if allCompliant bool == true
if (allCompliant == true)
{
if (File.Exists(MainEntry.thirdPartyStateXMLPath))
{
XDocument doc = XDocument.Load(MainEntry.thirdPartyStateXMLPath);
DateTime localCurrentTime = DateTime.Now;
DateTime utcCurrentTime = DateTime.UtcNow;
XElement root = new XElement("Compliance_Status");
root.Add(new XElement("Last_Known_Compliant_UTC", utcCurrentTime));
root.Add(new XElement("Last_Known_Compliant_LocalTime", localCurrentTime.ToString()));
doc.Element("Compliance_Items").Add(root);
doc.Save(MainEntry.thirdPartyStateXMLPath);
}
}
That renders the following XML:
<?xml version="1.0" encoding="utf-8"?>
<Compliance_Items>
<Compliance_Status>
<Last_Known_Compliant_UTC>2014-04-03T23:22:31.507088Z</Last_Known_Compliant_UTC>
<Last_Known_Compliant_LocalTime>4/3/2014 4:22:31 PM</Last_Known_Compliant_LocalTime>
</Compliance_Status>
</Compliance_Items>
This code generates the *Last_Known_Compliant_UTC* and *Last_Known_Compliant_LocalTime* elements and values. On subsequent runs of the code I want it to only replace the values of the existing elements, but as written now the following is re-created each time and keeps stacking in the XML:
<Compliance_Status>
<Last_Known_Compliant_UTC>2014-04-03T23:22:31.507088Z</Last_Known_Compliant_UTC>
<Last_Known_Compliant_LocalTime>4/3/2014 4:22:31 PM</Last_Known_Compliant_LocalTime>
</Compliance_Status>
How can I achieve the desired effect?
If you want to get a specific item and modify it you can do the following:
var xmlDocument = XDocument.Load("path");
var element = xmlDocument
.Descendants("Compliance_Status")
.FirstOrDefault(x => (DateTime)x.Element("Last_Known_Compliant_UTC") == someValue)
if(element != null)
/* update the value simply using element.Value = something
and save the xml file xmlDocument.Save("path") */
If you want to replace your element with another element you can use XElement.ReplaceWith
method.
0 commentaires:
Enregistrer un commentaire