gpt4 book ai didi

c# - 向 XML 文件添加新行

转载 作者:数据小太阳 更新时间:2023-10-29 02:53:40 25 4
gpt4 key购买 nike

我有一个创建 XML 日志的程序。首先,我检查 XML 是否存在,如果不存在,我使用以下代码创建它:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings))
{
writer.WriteStartDocument();
writer.WriteComment("Comment here.");
writer.WriteStartElement("Batch");
writer.WriteAttributeString("ID", BatchID);
writer.WriteAttributeString("Date", now);
writer.WriteStartElement("Step");
writer.WriteElementString("Name", step);
writer.WriteElementString("Success", success);
writer.WriteElementString("Message", message);
writer.WriteStartElement("Transaction");
writer.WriteElementString("ID", transID);
writer.WriteElementString("Details", details);

writer.WriteEndElement();
writer.WriteEndDocument();
}

这按预期工作并创建了文件:

<!--This log was created by the Application.-->
<Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93">
<Step>
<Name>Start Batch</Name>
<Success>1</Success>
<Message>Processing Batch</Message>
<Transaction>
<ID>N/A</ID>
<Details>N/A</Details>
</Transaction>
</Step>

所以第二次,它检查 XML 是否存在,它确实存在,所以与其创建它,不如直接添加它。我为此使用以下代码:

                XDocument xmlDoc = XDocument.Load(myXMLLog);
XElement root = new XElement("Batch");

root.Add(new XAttribute("ID", BatchID));
root.Add(new XAttribute("Date", now));
root.Add(new XElement("Step"));
root.Add(new XAttribute("Name", step));
root.Add(new XAttribute("Success", success));
root.Add(new XAttribute("Message", message));
root.Add(new XElement("Transaction"));
root.Add(new XAttribute("ID", transID));
root.Add(new XAttribute("Details", details));

xmlDoc.Element("Batch").Add(root);
xmlDoc.Save(myXMLLog);

所以现在我希望 XML 文件看起来像这样:

<!--This log was created by the Application.-->
<Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93">
<Step>
<Name>Start Batch</Name>
<Success>1</Success>
<Message>Processing Batch</Message>
<Transaction>
<ID>N/A</ID>
<Details>N/A</Details>
</Transaction>
</Step>
</Batch>
<Batch Date="1/22/2014 10:20:00 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d5aaa">
<Step>
<Name>Start Batch</Name>
<Success>1</Success>
<Message>Processing Batch</Message>
<Transaction>
<ID>N/A</ID>
<Details>N/A</Details>
</Transaction>
</Step>
</Batch>

但是我得到一个异常“重复属性”。

我做错了什么?

谢谢!

编辑:

XML 文档确实位于其顶部。有效的 XML 会很好,但不是最好的。

编辑编辑:

这是我的代码现在的样子:

            if (File.Exists(myXMLLog))
{
XDocument xmlDoc = XDocument.Load(myXMLLog);
XElement root = new XElement("Batch",
new XAttribute("ID", BatchID),
new XAttribute("Date", now),
new XElement("Step",
new XElement("Name", step),
new XElement("Success", success),
new XElement("Message", message),
new XElement("Transaction",
new XAttribute("IDTrans", transID),
new XAttribute("Details", details))));
xmlDoc.Root.Add(root);
xmlDoc.Save(myXMLLog);
}
else
{
using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings))
{
writer.WriteStartDocument();

writer.WriteComment("This log was created by the Application.");
writer.WriteStartElement("Batch");
writer.WriteAttributeString("ID", BatchID);
writer.WriteAttributeString("Date", now);
writer.WriteStartElement("Step");
writer.WriteElementString("Name", step);
writer.WriteElementString("Success", success);
writer.WriteElementString("Message", message);
writer.WriteStartElement("Transaction");
writer.WriteElementString("IDTrans", transID);
writer.WriteElementString("Details", details);

writer.WriteEndElement();
writer.WriteEndDocument();
}
}

我对 XML 的格式没问题,我现在的问题是在第一个 BATCH 中执行 BATCH

<Batch Date="1/22/2014 11:49:17 PM" ID="6966578b-b326-4f16-a315-1b4228a9fa42">
<Step><Name>Create DataTable</Name>
<Success>1</Success>
<Message>DataTable was Created and Populated</Message>
<Transaction>
<IDTrans>N/A</IDTrans>
<Details>0</Details>
</Transaction>
</Step>
<Batch Date="1/22/2014 11:49:17 PM" ID="6966578b-b326-4f16-a315-1b4228a9fa42">
<Step>
<Name>Send Email</Name>
<Success>1</Success>
<Message>Template Seleted and Filled</Message>
<Transaction Details="Email To: email@email.com Link: " IDTrans="243b0a3c-d8b7-49c3-b1d0-asdfsdsdfsdf"/>
</Step>
</Batch>.....

编辑最终版本

工作代码,所有答案的组合:

            if (File.Exists(myXMLLog))
{
XDocument xmlDoc = XDocument.Load(myXMLLog);
XElement root = new XElement("Batch",
new XAttribute("ID", BatchID),
new XAttribute("Date", now),
new XElement("Step",
new XElement("Name", step),
new XElement("Success", success),
new XElement("Message", message),
new XElement("Transaction",
new XAttribute("IDTrans", transID),
new XAttribute("Details", details))));
xmlDoc.Root.Add(root);
xmlDoc.Save(myXMLLog);
}
else
{
using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings))
{
writer.WriteStartDocument();
writer.WriteComment("This log was created by the Application.");
writer.WriteStartElement("Root");
writer.WriteStartElement("Batch");
writer.WriteAttributeString("ID", BatchID);
writer.WriteAttributeString("Date", now);
writer.WriteStartElement("Step");
writer.WriteElementString("Name", step);
writer.WriteElementString("Success", success);
writer.WriteElementString("Message", message);
writer.WriteStartElement("Transaction");
writer.WriteElementString("IDTrans", transID);
writer.WriteElementString("Details", details);
writer.WriteEndElement();
writer.Flush();
writer.WriteEndDocument();
}
}

我对结果很满意。如果有人对如何更好地格式化 XML 有建议,请这样做。谢谢大家。

最佳答案

您当前的代码将所有这些元素和属性添加到 root,而不是将一些属性添加到新创建的元素。

元素创建应该是这样的:

XElement root = new XElement("Batch",
new XAttribute("ID", BatchID),
new XAttribute("Date", now),
new XElement("Step",
new XElement("Name", step),
new XElement("Success", success),
new XElement("Message", message),
new XElement("Transaction",
new XElement("ID", transID),
new XElement("Details", details))));

关于c# - 向 XML 文件添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21298299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com