C# 将节点插入到XML文件中

C# 将节点插入到XML文件中,c#,xml,xpath,C#,Xml,Xpath,我正在尝试将一行/节点(如下所示)添加到XML中: <Import Project=".www\temp.proj" Condition="Exists('.www\temp.proj')" /> 虽然我不知道如何在XML中添加这一行,因为这是我第一次尝试直接编辑XML文件,但这个错误导致了额外的问题 我第一次尝试时出现以下错误: 根级别的C#“loadxml”数据无效。第1行,位置1' 知道这是一个著名的错误,有些人在这个链接中提供了多种方

我正在尝试将一行/节点(如下所示)添加到XML中:

<Import Project=".www\temp.proj" Condition="Exists('.www\temp.proj')" />
虽然我不知道如何在XML中添加这一行,因为这是我第一次尝试直接编辑XML文件,但这个错误导致了额外的问题

我第一次尝试时出现以下错误:

根级别的C#“loadxml”数据无效。第1行,位置1'

知道这是一个著名的错误,有些人在这个链接中提供了多种方法:

不幸的是,大多数解决方案都过时了,答案在这个案例中不起作用,我不知道如何在这个案例中应用其他解决方案

在该问题的链接上提供/接受答案:

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
    xml = xml.Remove(0, _byteOrderMarkUtf8.Length);
}
基本上它不起作用,因为
xml.StartsWith
似乎不再存在,同时
xml.Remove
也不存在


您能提供一段代码绕过错误并将该行添加到XML中吗

编辑:
注释部分提供了示例XML文件。

您可以使用官方的MSBuild库吗?()
我不确定只读取和编辑项目文件实际上需要哪个nuget包

我已尝试通过编程方式直接编辑MSBuild项目文件,但不推荐这样做。由于意外的变化,它经常中断。。。
MSBuild库在编辑项目文件和添加属性、项或导入等方面做得很好。

对于注释中发布的Xml,我使用了两种方法:

1-
XmlDocument

XmlDocument Proj = new XmlDocument();
Proj.Load(file);
XmlElement root = Proj.DocumentElement;
//Create node
XmlNode node = Proj.CreateNode(XmlNodeType.Element, "Import", null);

//create attributes
XmlAttribute attrP = Proj.CreateAttribute("Project");
attrP.Value = ".www\\temp.proj";

XmlAttribute attrC = Proj.CreateAttribute("Condition");
attrC.Value = "Exists('.www\\temp.proj')";

node.Attributes.Append(attrP);
node.Attributes.Append(attrC);

//Get node PropertyGroup, the new node will be inserted before it
XmlNode pG = Proj.SelectSingleNode("/Project/PropertyGroup");
root.InsertBefore(node, pG);

Console.WriteLine(root.OuterXml);
2-Linq到Xml,使用

要为
XDocument
添加的命名空间:

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
两种解决方案给出的结果相同,但最后一种方法很简单


我希望这对您有所帮助。

您能分享原始xml吗?是的,请给我一分钟。在OneDrive:(名称不同,无论您使用哪个名称,我都可以编辑问题以使其匹配)不,我有足够的MSBuild内容,这里我的问题是关于提到的问题。谢谢,抱歉,迟到了一点,请给我一点时间看看你漂亮的答案+1喜欢这两种方法,它们清楚地显示了所需的步骤。如果你能回答,有两个相关的问题,第一个是它是如何绕过这个错误的?XML是相同的,并且似乎在开始(加载)时使用了相同的方法,它如何绕过关于第一个字符的疯狂错误?第二,如果需要,如何在关闭根/包装器元素()之前移动插入的行?就在最后一行之前?再次感谢。@Kasrak很抱歉回答得太晚,我将检查并回答您1个问题:它是如何绕过该错误的?您使用了
.LoadXml(filePath)
而不是
.Load(filePath)
,因为
.LoadXml(filePath)
需要像参数一样的xml字符串,而不是文件路径。2问题:对于
XmlDocument
,使用
.InsertAfter()
代替
.InsertBefore()
类似于
root.InsertAfter(节点,pG)
对于
XDocument
,使用
.Add()
代替
.AddFirst()
类似
XDocument.Root.Add(新…
)。我希望这能回答你的问题。
XmlDocument Proj = new XmlDocument();
Proj.Load(file);
XmlElement root = Proj.DocumentElement;
//Create node
XmlNode node = Proj.CreateNode(XmlNodeType.Element, "Import", null);

//create attributes
XmlAttribute attrP = Proj.CreateAttribute("Project");
attrP.Value = ".www\\temp.proj";

XmlAttribute attrC = Proj.CreateAttribute("Condition");
attrC.Value = "Exists('.www\\temp.proj')";

node.Attributes.Append(attrP);
node.Attributes.Append(attrC);

//Get node PropertyGroup, the new node will be inserted before it
XmlNode pG = Proj.SelectSingleNode("/Project/PropertyGroup");
root.InsertBefore(node, pG);

Console.WriteLine(root.OuterXml);
XDocument xDocument = XDocument.Load(file);

xDocument.Root.AddFirst(new XElement("Import", 
    new XAttribute[] 
    { 
        new XAttribute("Project", ".www\\temp.proj"), 
        new XAttribute("Condition", "Exists('.www\\temp.proj')") 
    }));

Console.WriteLine(xDocument);
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;