gpt4 book ai didi

c# - 尝试写入 C# 的现有 XML 文件

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

非常感谢您

这是原始的 XML 文件

<my:Incident>
<my:Category>This is for Category</my:Category>
<my:Status>`Status is Close`</my:Status>
<my:Description>`This is the description part</my:Description>
</my:Incident>

我想在 my:Incident 下添加其他字段

这是一个例子:

<my:Incident>
<my:Category>This is for Category</my:Category>
<my:Status>`Status is Close`</my:Status>
<my:SummaryDescription>This is the summary</my:SummaryDescription>
<my:Description>`This is the description part</my:Description>
</my:Incident>

我尝试实现但收到此错误消息:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

public void writerXMLTest(string fileName)
{

if (!File.Exists(fileName))
{
XmlTextWriter writer = new XmlTextWriter(fileName, null);
writer.WriteStartElement("my:Incident");
writer.WriteEndElement();
writer.Close();
}

XDocument doc = XDocument.Load(fileName);
XElement demoNode = new XElement("my:Incident");
demoNode.Add(new XElement("my:SummaryDescription", "Test Test"));


Console.WriteLine("I write it!!!!!");
}

如果有人能指导我在我的代码中哪里做错了,我将不胜感激。


我稍微修改了代码。但是现在我无法将它写入现有的 XML 文件

这是我的代码:

public void writerXMLTest(string fileName)
{

if (!File.Exists(fileName))
{
XmlTextWriter writer = new XmlTextWriter(fileName, null);
writer.WriteStartElement("Incident", "my");
writer.WriteEndElement();
writer.Close();
}

XDocument doc = XDocument.Load(fileName);
XElement demoNode = new XElement("SummaryDescription", "Test Test");


Console.WriteLine("I write it!!!!!");
}

最佳答案

这是错误的:

 writer.WriteStartElement("my:Incident");

这是对的:

 writer.WriteStartElement("Incident", "blablablaSpace:my");

编辑:

 writer.WriteStartElement("Incident", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-09-22T20:42:56:my");

关于c# - 尝试写入 C# 的现有 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8437495/

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