XML to LinQ modify attribute value; best way

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,
I need to read an XML file that has these 2 sides. It can happen that a side does not have the element "PROCESSSTEP".
If the element exists, I have to change the attribute from "HANDLING" it and save it.
<PRODUCT>
<NAME value="RECEIVER" />
<SIDE1>
<POSITIONS>
<POSITION>
<NUMBER value="1" />
<ACTIVE value="1" />
<PROCESSSTEP>
<STEP value="MarkRed">
<HANDLING value="4" />
</STEP>
<STEP value="MarkGreen">
<HANDLING value="0" />
<PRINTGROWTHMINVALUE value="F" />
</STEP>
</PROCESSSTEP>
</POSITION>
</SIDE1>
<SIDE2>
<POSITIONS>
<POSITION>
<NUMBER value="1" />
<ACTIVE value="1" />
</POSITION>
</SIDE2>
</PRODUCT>
How can I do this?
It can be missing for one side, for both sides or just available, all possibilities.
My attempt to make it work for side 1, side 2 doesn't because the element is missing, although it's written like this.
?.Element("POSITION")?.Element("PROCESSSTEP")?.Elements()
Does anyone have a solution, any suggestions on how to solve it?
Thanks in advance.

With best regards Markus
XDocument productXML = XDocument.Load(productFilePath);
int entries = 1;
bool changed = false;

for (int side = 1; side <= 2; side++)
{
foreach (XElement xePosition in programXML.Element($"SIDE{side}").Element("POSITIONS")?.Element("POSITION")?.Element("PROCESSSTEP")?.Elements())
{
if (xePosition.Attribute("value")?.Value == "MarkGreen")
{
foreach (XElement xeStep in xePosition?.Elements())
{
switch (xeStep?.Name.LocalName)
{
case "HANDLING":
if (newParameterset1.Length > 0)
{
xeStep.SetAttributeValue("value", "NewHandlingValue");
changed = true;

Continue reading...
 

Similar threads

M
Replies
0
Views
67
Markus Freitag
M
M
Replies
0
Views
89
Markus Freitag
M
M
Replies
0
Views
79
Markus Freitag
M
Back
Top