none
XML - Change element value RRS feed

  • Question

  • Hello,
    I have 1000 XML files to adjust.
    I have to subtract the index by 1 at each position.
    Each file has two or more groups.
    newValue = value - 1;
    
    <POSITION>
    	<INDEX value="1" />      to <INDEX value="0" />
    	<NUMBER value="1" />
    	<XPOS value="45.598" />
    	<YPOS value="61.352" />
    	<ZPOS value="0" />
    </POSITION>
    
    <POSITION>
    	<INDEX value="2" />      to <INDEX value="1" />
    	<NUMBER value="2" />
    	<XPOS value="65.598" />
    	<YPOS value="81.352" />
    	<ZPOS value="0" />
    </POSITION>

    -WHITEGROUP
    -BLACKGROUP
    ..
    - XXXX_GROUP

    Read in, save the changes.
    What is the best way to do this with XDocument (XElement).
    Before saving the file, make a temporary backup of the previous file.
    Does anyone have a good tip.
    Thanks in advance.
    With best regards Markus
    <!-- Original -->
    
    <ROOT>
    	<PRODUCTLAYOUT>
    		<IDENTNR value="0" />
    		<WHITEGROUP>
    			<POSITIONS>
    				<POSITION>
    					<INDEX value="1" />
    					<NUMBER value="1" />
    					<XPOS value="45.598" />
    					<YPOS value="61.352" />
    					<ZPOS value="0" />
    				</POSITION>
    				<POSITION>
    					<INDEX value="2" />
    					<NUMBER value="2" />
    					<XPOS value="65.598" />
    					<YPOS value="81.352" />
    					<ZPOS value="0" />
    				</POSITION>
    				...
    				<POSITION>
    					<INDEX value="99" />
    					<NUMBER value="99" />
    					<XPOS value="465.598" />
    					<YPOS value="481.352" />
    					<ZPOS value="0" />
    				</POSITION>
    			</POSITIONS>
    		<WHITEGROUP>	
    		<BLACKGROUP>
    			<POSITIONS>
    				<POSITION>
    					<INDEX value="1" />
    					<NUMBER value="1" />
    					<XPOS value="45.598" />
    					<YPOS value="61.352" />
    					<ZPOS value="0" />
    				</POSITION>
    				<POSITION>
    					<INDEX value="2" />
    					<NUMBER value="2" />
    					<XPOS value="65.598" />
    					<YPOS value="81.352" />
    					<ZPOS value="0" />
    				</POSITION>
    				...
    				<POSITION>
    					<INDEX value="99" />
    					<NUMBER value="99" />
    					<XPOS value="465.598" />
    					<YPOS value="481.352" />
    					<ZPOS value="0" />
    				</POSITION>
    			</POSITIONS>
    		<BLACKGROUP>


    Friday, March 27, 2020 3:58 PM

Answers

  • Hi Markus,

    There is no mention of groups in my code, but all INDEX in white group and black group have been changed.

    And it does not modify the source file, but creates a new file, so even if it fails, it will not cause data loss, so it should meet your needs.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 31, 2020 1:19 AM

All replies

  • Hi Markus,

    Thank you for posting here.

    You can use XmlDocument to do this, try the following code to get it.

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(@"D:\test\xml\test.xml");
    
                XmlNodeList xmlNodeList = xmlDocument.GetElementsByTagName("INDEX");
                foreach (XmlNode item in xmlNodeList)
                {
                    XmlAttributeCollection xmlAttributeCollection = item.Attributes;
                    XmlNode xml =  xmlAttributeCollection.GetNamedItem("value");
                    xml.InnerText = (int.Parse(xml.InnerText) - 1).ToString() ;              
                }
                xmlDocument.Save(@"D:\test\xml\test1.xml");

    Result:

    As we can see, all index values are subtracted by 1.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 30, 2020 2:21 AM
  • Hi Timon,

    Looks good, thanks. I get 1.000 files from my client at the end of the week, then I test it.

    It is important to replace the indexes of all Groups (Black, White and so one).

    I think similarly I could do it with XDocument. I have to read the directory and modify all xml files.

    If it works as expected, i put it on answered. 

    Greetings Markus

    Monday, March 30, 2020 10:17 AM
  • Hi Markus,

    There is no mention of groups in my code, but all INDEX in white group and black group have been changed.

    And it does not modify the source file, but creates a new file, so even if it fails, it will not cause data loss, so it should meet your needs.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 31, 2020 1:19 AM