I'm trying to search for a specific XAttribute and XElement.
<IcMDetails ServerGroup="ABC">
<IncidentId>984513515</IncidentId>
<Title>today is a great day</Title>
<Description>this is a test</Description>
<State>Active</State>
<Severity>2</Severity>
<DateCreated>2016-12-12</DateCreated>
</IcMDetails>
<IcMDetails ServerGroup="XYZ">
<IncidentId>6845613</IncidentId>
<Title>today is a great day</Title>
<Description>this is a test</Description>
<State>Active</State>
<Severity>2</Severity>
<DateCreated>2016-12-08</DateCreated>
</IcMDetails>
<IcMDetails ServerGroup="ABC">
<IncidentId>12345345</IncidentId>
<Title>today is a great day</Title>
<Description>this is a test</Description>
<State>Resolved</State>
<Severity>2</Severity>
<DateCreated>2016-12-08</DateCreated>
</IcMDetails>
Where: ServerGroup = ABC State = Active
Once I have found the specific Child record, I want to update the State. I assume I can do something like:
public static void Update()
{
string filePath = "XML.xml"
XDocument doc = XDocument.Load(filePath);
var items = from i in doc.Descendants("IcMDetails")
select i;
foreach(var item in items)
{
item.Element("State").Value = "NewValue";
}
}
I used the following to read the XML file. However, I am unable to read the XAttribute value.
public static void Read()
{
XmlTextReader reader = new XmlTextReader("TFS_Tickets.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Attribute:
Console.WriteLine(reader.Value);
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}