Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an odd problem with .net framework 4 appending anything to an xml document.

I have a small (35k) xml file that I am trying to add comments to. It used to work fine, but now when I try to add anything to the file, it gets stuck when I append anything to the xml document. I can edit the comments with no problem.

The Call Stack indicates that it may have something to do with the XmlElementListListener.OnListChanged. I'm not sure why, because I'm not using any xml events.

Thanks for your assistance.

C#
 	mscorlib.dll!System.MulticastDelegate.RemoveImpl(System.Delegate value) + 0xaa bytes	
 	System.Xml.dll!System.Xml.XmlElementListListener.OnListChanged(object sender, System.Xml.XmlNodeChangedEventArgs args) + 0xe6 bytes	
 	System.Xml.dll!System.Xml.XmlNode.AfterEvent(System.Xml.XmlNodeChangedEventArgs args) + 0x27 bytes	
 	System.Xml.dll!System.Xml.XmlNamedNodeMap.AddNode(System.Xml.XmlNode node) + 0x104 bytes	
 	System.Xml.dll!System.Xml.XmlAttributeCollection.AddNode(System.Xml.XmlNode node) + 0x42 bytes	
 	System.Xml.dll!System.Xml.XmlAttributeCollection.Append(System.Xml.XmlAttribute node) + 0x96 bytes	
>	cmsoClashReports.dll!Camansol.Camansys.CMSGlobal.ClashReports.ClashComments.Add(Camansol.Camansys.CMSGlobal.ClashReports.Status _resultStatus) Line 322 + 0x34 bytes	C#



This is the xml snippet:

HTML
<exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" units="m" filename="" filepath="">
  <batchtest name="Report" internal_name="Report" units="m">
    <clashtests>
<!-- irrelevant data -->
    <summmary />
    <clashreults>
<clashresult name="Clash4" guid="0091a62c-fb5e-4aa7-82eb-0c5814d58bdc" href="Piping_files\cd000001.jpg" status="reviewed" distance="-0.004">
            <description>Hard</description>
            <resultstatus>Reviewed</resultstatus>
            <clashpoint>
              <pos3f x="25.960" y="19.206" z="4.018" />
            </clashpoint>
            <createddate>
              <date year="2016" month="5" day="2" hour="22" minute="45" second="52" />
            </createddate>
            <clashobjects />
            <comments> <!--  ATTEMPTING TO ADD NEW COMMENT TO THIS NODE -->
              <comment id="10000" status="new">
                <body>Replace Flanges</body>
                <user>usename</user>
                <createddate>
                  <date year="2016" month="5" day="9" hour="15" minute="0" second="26" />
                </createddate>
              </comment>
            </comments>
          </clashresult>
    </clashresults>
    </clashtests>
</batchtest>
</exchange>


This is the code:
C#
public void Add(Status _resultStatus)
        {
            int iNextID = 10000 + this.GetNextID; ;

            DateTime _dtComment = DateTime.Now;

            XmlDocument _xdClashReport = this.xmlClashComments.OwnerDocument;
            XmlElement _nodNewComment = _xdClashReport.CreateElement("comment");
            XmlElement _nodNewCommentUser = _xdClashReport.CreateElement("user");
            XmlElement _nodNewCommentBody = _xdClashReport.CreateElement("body");
            XmlElement _nodNewCommentCreatedDate = _xdClashReport.CreateElement("createddate");
            XmlElement _nodNewCommentDate = _xdClashReport.CreateElement("date");


            XmlAttribute _xatrNewCommentID = _xdClashReport.CreateAttribute("id");
            XmlAttribute _xatrNewCommentStatus = _xdClashReport.CreateAttribute("status");
            _nodNewComment.Attributes.Append(_xatrNewCommentID); //Application gets stuck here
            _nodNewComment.Attributes.Append(_xatrNewCommentStatus);
            _nodNewComment.SetAttribute("id", iNextID.ToString());
            _nodNewComment.SetAttribute("status", _resultStatus.ToString().ToLower());

            XmlAttribute _xatrNewCommentDateYear = _xdClashReport.CreateAttribute("year");
            XmlAttribute _xatrNewCommentDateMonth = _xdClashReport.CreateAttribute("month");
            XmlAttribute _xatrNewCommentDateDay = _xdClashReport.CreateAttribute("day");
            XmlAttribute _xatrNewCommentDateHour = _xdClashReport.CreateAttribute("hour");
            XmlAttribute _xatrNewCommentDateMinute = _xdClashReport.CreateAttribute("minute");
            XmlAttribute _xatrNewCommentDateSecond = _xdClashReport.CreateAttribute("second");


            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateYear);
            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateMonth);
            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateDay);
            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateMonth);
            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateHour);
            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateMonth);
            _nodNewCommentDate.Attributes.Append(_xatrNewCommentDateMinute);

            _nodNewCommentDate.SetAttribute("year", _dtComment.Year.ToString());
            _nodNewCommentDate.SetAttribute("month", _dtComment.Month.ToString());
            _nodNewCommentDate.SetAttribute("day", _dtComment.Day.ToString());
            _nodNewCommentDate.SetAttribute("hour", _dtComment.Hour.ToString());
            _nodNewCommentDate.SetAttribute("minute", _dtComment.Minute.ToString());
            _nodNewCommentDate.SetAttribute("second", _dtComment.Second.ToString());

            _nodNewCommentUser.InnerText = Environment.UserName;
            _nodNewCommentBody.InnerText = "New Comments";
            _nodNewCommentCreatedDate.AppendChild(_nodNewCommentDate);

            _nodNewComment.AppendChild(_nodNewCommentBody);
            _nodNewComment.AppendChild(_nodNewCommentUser);
            _nodNewComment.AppendChild(_nodNewCommentCreatedDate);

            this.xmlClashComments.AppendChild(_nodNewComment);

            ClashComment _newClashComment = new ClashComment();
            _newClashComment.xmlClashComment = _nodNewComment;

            this.List.Add(_newClashComment);


            if (OnClashCommentAdded != null)
            {
                OnClashCommentAdded(this, new ClashCommentsEventArgs());
            }
        }


What I have tried:

Tried creating a new project
upgrading to .net framework 4.5
reverting to previous version of my code
Posted
Updated 29-Jul-16 7:21am
v2

1 solution

Quote:
The Call Stack indicates that it may have something to do with the XmlElementListListener.OnListChanged. I'm not sure why, because I'm not using any xml events.
Appending to the file is your OnListChanged event.

If same code used to work, check that the XML file is bot corrupted.

And use the debugger to see exactly when and where to the problem arise.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
macgyver700210 30-Jul-16 10:15am    
Thanks for your help. Debugging is an integral part of my programming process. The issue is that I do not have the source code for the system.xml namespace. If I did, I could probably fix it.

I am leaning towards it being a bug. I believe it may have something to do with the third party api I am using because it works as expected in a test application where I am not referencing that api. (I did that after this post). I will investigate further when I'm back at the office on Tuesday. Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900