将元素,属性附加到xmldocument会陷入循环。 [英] Appending elements, attributes to xmldocument gets stuck in loop.

查看:58
本文介绍了将元素,属性附加到xmldocument会陷入循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题.net框架4将任何内容附加到xml文档。



我有一个小的(35k)xml文件,我正在尝试添加评论。它曾经工作正常,但现在当我尝试向文件添加任何内容时,当我向xml文档追加任何内容时它会卡住。我可以毫无问题地编辑评论。



调用堆栈表明它可能与XmlElementListListener.OnListChanged有关。我不知道为什么,因为我没有使用任何xml活动。



感谢你的帮助。



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.

 	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#







这是xml片段:






This is the xml snippet:

<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:

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());
            }
        }





我的尝试:



尝试创建一个新项目

升级到.net framework 4.5

恢复到以前版本的代码



What I have tried:

Tried creating a new project
upgrading to .net framework 4.5
reverting to previous version of my code

推荐答案

Quote:

调用堆栈表明它可能与XmlElementListListener.OnListChanged有关。我不知道为什么,因为我没有使用任何xml事件。

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.

附加到文件是你的 OnListChanged 事件。



如果使用相同的代码,请检查XML文件是否已损坏。



并使用调试器查看确切的时间和问题出在哪里。



调试器允许你逐行跟踪执行,检查变量,你会看到有一点它停止做什么你期待。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

当代码没有达到预期的效果时,你就会接近一个错误。

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.


这篇关于将元素,属性附加到xmldocument会陷入循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆