C# Xml根节点未关闭

C# Xml根节点未关闭,c#,xml,root,stack-overflow,C#,Xml,Root,Stack Overflow,我正在使用xmlwriter创建一个xml文档。xml文档如下所示: <?xml version="1.0" encoding="utf-8" ?> <ExceptionsList /> string formatDate = DateTime.Now.ToString("d-MMM-yyyy"); XmlTextWriter xmlWriter = new XmlTextWriter(xmlfileName, Encoding.UTF8); xmlWriter

我正在使用xmlwriter创建一个xml文档。xml文档如下所示:

<?xml version="1.0" encoding="utf-8" ?> 
<ExceptionsList /> 
string formatDate = DateTime.Now.ToString("d-MMM-yyyy");

XmlTextWriter xmlWriter = new XmlTextWriter(xmlfileName, Encoding.UTF8);

xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 3;
xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("ExceptionsList"); // ExceptionsList (Root) Element

xmlWriter.WriteEndElement(); // End of ExceptionsList (Root) Element

xmlWriter.WriteEndDocument();

xmlWriter.Flush();
xmlWriter.Close();
XDocument xml = XDocument.Load(xmlFileName);
XElement root = xml.Root;

root.Add(new XElement("Exception",
    new XElement("Exception Type", exceptionType),
    new XElement("Exception Message", exceptionMessage),
        new XElement("InnerException", innerException),
    new XElement("Comment", comment)));

xml.Save(xmlFileName);
XDocument xml = new XDocument();
xml.Add(new XElement("Exception",
    new XElement("ExceptionType", "Exception"),
    new XElement("ExceptionMessage", 
        new XElement("InnerException", "innerException")),
    new XComment("some comment")));

xml.Save("sample2.xml");
我向根节点添加如下内容:

<?xml version="1.0" encoding="utf-8" ?> 
<ExceptionsList /> 
string formatDate = DateTime.Now.ToString("d-MMM-yyyy");

XmlTextWriter xmlWriter = new XmlTextWriter(xmlfileName, Encoding.UTF8);

xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 3;
xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("ExceptionsList"); // ExceptionsList (Root) Element

xmlWriter.WriteEndElement(); // End of ExceptionsList (Root) Element

xmlWriter.WriteEndDocument();

xmlWriter.Flush();
xmlWriter.Close();
XDocument xml = XDocument.Load(xmlFileName);
XElement root = xml.Root;

root.Add(new XElement("Exception",
    new XElement("Exception Type", exceptionType),
    new XElement("Exception Message", exceptionMessage),
        new XElement("InnerException", innerException),
    new XElement("Comment", comment)));

xml.Save(xmlFileName);
XDocument xml = new XDocument();
xml.Add(new XElement("Exception",
    new XElement("ExceptionType", "Exception"),
    new XElement("ExceptionMessage", 
        new XElement("InnerException", "innerException")),
    new XComment("some comment")));

xml.Save("sample2.xml");
这也给了我一个运行时堆栈溢出错误

任何帮助都将不胜感激


提前谢谢

您的代码是正确的,您不需要更改
异常列表
元素的关闭方式

xmlWriter.WriteStartElement("ExceptionsList"); // ExceptionsList (Root) Element

xmlWriter.WriteStartElement("Exception"); // An Exception element
xmlWriter.WriteEndElement();

xmlWriter.WriteEndElement(); // End of ExceptionsList (Root) Element
在第二个代码片段中,您需要从元素名称中删除这些空格,因为XML规范禁止这样做,并将元素添加到
XDocument
实例中,如下所示:

<?xml version="1.0" encoding="utf-8" ?> 
<ExceptionsList /> 
string formatDate = DateTime.Now.ToString("d-MMM-yyyy");

XmlTextWriter xmlWriter = new XmlTextWriter(xmlfileName, Encoding.UTF8);

xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 3;
xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("ExceptionsList"); // ExceptionsList (Root) Element

xmlWriter.WriteEndElement(); // End of ExceptionsList (Root) Element

xmlWriter.WriteEndDocument();

xmlWriter.Flush();
xmlWriter.Close();
XDocument xml = XDocument.Load(xmlFileName);
XElement root = xml.Root;

root.Add(new XElement("Exception",
    new XElement("Exception Type", exceptionType),
    new XElement("Exception Message", exceptionMessage),
        new XElement("InnerException", innerException),
    new XElement("Comment", comment)));

xml.Save(xmlFileName);
XDocument xml = new XDocument();
xml.Add(new XElement("Exception",
    new XElement("ExceptionType", "Exception"),
    new XElement("ExceptionMessage", 
        new XElement("InnerException", "innerException")),
    new XComment("some comment")));

xml.Save("sample2.xml");

我认为问题在于:

    new XElement("Exception Type", exceptionType),
     new XElement("Exception Message", exceptionMessage),

Exception-Type
Exception-Mesage
都不是xml元素的有效名称。当然,如果您使用这个(注定的)方法来记录错误。。。堆栈溢出。

我很困惑。。。你为什么说它没有适当地关闭?我觉得这很接近…真的不明白你的意思。堆栈溢出发生在哪里?为什么不希望关闭根元素?为什么希望XmlWriter产生无效的输出?是什么阻止了您将第二个代码片段(或合适的调用)放在ExceptionList的开始和结束元素调用之间?是的,这就是导致stackoverflow的原因。我知道了。TBH,我从未在xml标记中使用空格。我不知道这次为什么这么做:哦,谢谢你的评论!