C# XDocument replace attributes values

Markus Freitag 3,791 Reputation points
2023-01-14T08:32:42.57+00:00

Hello,

I have it and I want to change and replace attributes.

Works well with XmlDocument. Please see attached test files.

Product_xml.TXT

Product_xml_NEW.TXT

Product_Complex_xml.TXT

Convert_to_XDocument.txt

private void btnXMLReplaceAttribute_Click(object sender, EventArgs e)
{
	XmlDocument xmlDocument = new XmlDocument();
	xmlDocument.Load(@"..

	XmlNodeList xmlNodeList = xmlDocument.GetElementsByTagName("LABEL");
	foreach (XmlNode item in xmlNodeList)
	{
		XmlAttributeCollection xmlAttributeCollection = item.Attributes;
		XmlNode xml = xmlAttributeCollection.GetNamedItem("value");

		//xml.InnerText = (int.Parse(xml.InnerText) - 1).ToString();
		xml.InnerText = "NEW String";
	}
	xmlDocument.Save(@"..
}

Two questions. How can I solve this with XDocument? I need XDocument! How can I filter it if the element name occurs multiple times. I only want to change the CAD elements

<POSITION>
	<ALTLABEL value="" />
	<ANGLE value="0" />
	<LABEL value="M22" />   // YES do it!


<SIDE2>
	<LABEL value="GlobalM22" />   // No not
	<POSITIONS>
		<POSITION>



// --------
<ROOT>
	<PANEL_CAD>
		<XSIZE value="230" />
		<YSIZE value="170" />
		<SIDE1>
			<POSITIONS>
				<POSITION>
					<ALTLABEL value="" />
					<ANGLE value="0" />
					<LABEL value="M22" />
					<XOFFSET value="0" />
					<XPOS value="69.897" />
					<YOFFSET value="0" />
					<YPOS value="86.827" />
				</POSITION>
				<POSITION>
					<ALTLABEL value="" />
					<ANGLE value="0" />
					<LABEL value="M22" />
					<XOFFSET value="0" />
					<XPOS value="69.897" />
					<YOFFSET value="0" />
					<YPOS value="86.827" />
				</POSITION>
			</POSITIONS>
		</SIDE1>
		<SIDE2>
		    <LABEL value="GlobalM22" />
			<POSITIONS>
				<POSITION>
					<ALTLABEL value="" />
					<ANGLE value="0" />
					<LABEL value="M22" />
					<XOFFSET value="0" />
					<XPOS value="69.897" />
					<YOFFSET value="0" />
					<YPOS value="86.827" />
				</POSITION>
				<POSITION>
					<ALTLABEL value="" />
					<ANGLE value="0" />
					<LABEL value="M22" />
					<XOFFSET value="0" />
					<XPOS value="69.897" />
					<YOFFSET value="0" />
					<YPOS value="86.827" />
				</POSITION>
			</POSITIONS>
		</SIDE2>
	</PANEL_CAD>
</ROOT>

Thank you for your help and the solutions.

Developer technologies | ASP.NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2023-01-14T09:30:36.0533333+00:00

    Try something like this:

    using System.Xml.XPath;
    
    . . .
    
    XDocument xdoc = XDocument.Load( @"..." );
    
    var labels = xdoc.Root.XPathSelectElements( "/ROOT/PANEL_CAD/*/POSITIONS/POSITION/LABEL" );
    foreach( XElement label in labels )
    {
        label.SetAttributeValue( "value", "NEW String" );
    }
    
    xdoc.Save( @"..." );
    

    The path that appears in XPathSelectElements can be adjusted for specific needs.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.