We help IT Professionals succeed at work.
Get Started

XML structure change after updating data in XML

hallhu asked
Last Modified: 2012-06-27
Hi experts,

Can you please help me with the problem regarding XML data updating. I have a problem that the structure of the XML changes after I make an update in XML.
Exactly: From

    <property id="32527" fileAssociated="0">

    <property id="32527" fileAssociated="0">NAE-01:NAE-01/MSTP_2.FX07_01nviTemp_Average</property>

I changed the inner string value and the structure is change also.

 I attached the code, the original and the updated XML file. What I'm doing wrong?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;

namespace NAE_BACNet_Object_Change
    public partial class Main : Form
        public Main()

        private void btnConvert_Click(object sender, EventArgs e)
            XElement XObjectsElements = XElement.Load(@"export.xml");

            string strFullReference = "";
            string strObjectName = "";

            foreach (XElement XObjectElement in XObjectsElements.Nodes().OfType<XElement>()) 
                if ((string)XObjectElement.Attribute("classid") == "502" | (string)XObjectElement.Attribute("classid") == "519")
                    foreach (XElement XPropertyElement in XObjectElement.Nodes().OfType<XElement>())
                        if ((string)XPropertyElement.Attribute("id") == "2390") { strObjectName = XPropertyElement.Value; }
                        if ((string)XPropertyElement.Attribute("id") == "32527")
                            strFullReference = XPropertyElement.Value;

                            int lastDotPosition = strFullReference.LastIndexOf('.');
                            string strNode = strFullReference.Substring(0, lastDotPosition);
                            string strObject = strFullReference.Substring(lastDotPosition + 1, strFullReference.Length - lastDotPosition - 1);

                            if (lastDotPosition != -1 && strObjectName != strObject)
                                lstLog.AppendText(String.Format("{0} -> {1}.{2}{3}", XPropertyElement.Value, strNode, strObjectName, Environment.NewLine));
                                XPropertyElement.SetValue(strNode + strObjectName);
                                lstLog.AppendText(String.Format("{0} -> {1}{2}", XPropertyElement.Value, "No Chnage", Environment.NewLine));

Open in new window

Watch Question
Most Valuable Expert 2011
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE