为什么我的代码不想再添加子节点? (XML)

时间:2011-02-27 07:58:50

标签: c# xml .net-4.0 xmldocument xmlnode

我正在编写一个程序来翻译游戏。原始字符串和翻译后的字符串将保存到XmlDocument中,当用户单击按钮时,可以将其保存到文件中。

一切正常,直到达到一定数量的节点(30?)或直到达到一定的大小(8192字节?),然后才停止向XmlDocument添加节点。

我的代码:http://lesderid.pastebin.com/zgcT9PVu

XML文件:@character.lua.Decoded.VOQ.xml

1 个答案:

答案 0 :(得分:2)

似乎没有任何问题,我尝试了你的代码,我得到了正确的输出..所以问题是在其他地方。


我尝试了这个(在LINQPAD中)并获得了100个元素(10,349个字节)

XmlDocument XmlDoc;
XmlElement mainStringsNode;


void Main()
{
    XmlDoc = new XmlDocument(); 
    XmlNode xmlDeclarationNode = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); 
    XmlDoc.AppendChild(xmlDeclarationNode); 
    mainStringsNode = XmlDoc.CreateElement("Strings");  
    XmlDoc.AppendChild(mainStringsNode);    
    var docWriter = new StringWriter(); 
    XmlDoc.Save(docWriter);
    for(int i=0; i < 100; i++)      AddStringChild(i, "satr", "edited");    
    XmlDoc.Dump();      
}
private void AddStringChild(int id, string originalString, string editedString)
{
        XmlNode stringNode = XmlDoc.CreateElement("String");
        var posAttribute = XmlDoc.CreateAttribute("position");
        posAttribute.Value = id.ToString();
        if (stringNode.Attributes != null) stringNode.Attributes.Append(posAttribute);
        mainStringsNode.AppendChild(stringNode);
        var originalStringNode = XmlDoc.CreateElement("OriginalString");
        originalStringNode.AppendChild(XmlDoc.CreateTextNode(originalString));
        stringNode.AppendChild(originalStringNode);
        var editedStringNode = XmlDoc.CreateElement("EditedString");
        editedStringNode.AppendChild(XmlDoc.CreateTextNode(editedString));
        stringNode.AppendChild(editedStringNode);
}

输出:

<?xml version="1.0" encoding="UTF-8"?><Strings><String position="0"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="1"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="2"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="3"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="4"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="5"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="6"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="7"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="8"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="9"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="10"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="11"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="12"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="13"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="14"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="15"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="16"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="17"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="18"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="19"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="20"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="21"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="22"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="23"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="24"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="25"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="26"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="27"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="28"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="29"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="30"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="31"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="32"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="33"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="34"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="35"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="36"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="37"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="38"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="39"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="40"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="41"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="42"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="43"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="44"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="45"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="46"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="47"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="48"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="49"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="50"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="51"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="52"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="53"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="54"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="55"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="56"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="57"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="58"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="59"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="60"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="61"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="62"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="63"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="64"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="65"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="66"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="67"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="68"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="69"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="70"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="71"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="72"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="73"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="74"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="75"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="76"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="77"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="78"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="79"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="80"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="81"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="82"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="83"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="84"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="85"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="86"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="87"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="88"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="89"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="90"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="91"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="92"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="93"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="94"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="95"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="96"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="97"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="98"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String><String position="99"><OriginalString>satr</OriginalString><EditedString>edited</EditedString></String></Strings>