添加元素的XML文件元素、文件、XML

2023-09-04 05:11:12 作者:性感小甜甜

我想添加和删除从C#.csproj的文件元素。该文件部分,如下所示。可有人告诉我怎么可以做以下两件事情?

在添加元素,如下图所示(这行,我想添加 这种) 在删除一个元素。例如,假设我想删除的行我 已表示如下。

 < XML版本=1.0编码=UTF-8&GT?;
<项目ToolsVersion =3.5DefaultTargets =建设
的xmlns =htt​​p://schemas.microsoft.com/developer/msbuild/2003>
   <的PropertyGroup>
      <结构状态='$(配置)'==''>调试和LT; /结构>
< /的PropertyGroup>
< ItemGroup>
   <参考包括=System.Data这/>
   <参考包括=System.Deployment/>
< / ItemGroup>
< ItemGroup>
   <编译包括=生成\ DatabaseContext.cs/>
   <编译包括=生成\ EntityClasses.cs/>
   <编译包括=生成\ Extensions.cs/>
   <编译包括=架构\ Column.cs/>
   <编译包括=架构\ EntityRef.cs/>
   <编译包括=SerializedData \ Tables.xml/> //我想补充这
< / ItemGroup>
<导入项目=$(MSBuildToolsPath)\ Microsoft.CSharp.targets/>
< /项目>
 

解决方案

您可以通过以下方式添加您的指示行:

 的XNamespace NS =htt​​p://schemas.microsoft.com/developer/msbuild/2003;
XDOC的XDocument = XDocument.Load(文件名);

。VAR B = xDoc.Descendants(NS +编译)第一();

b.Parent.Add(
    新的XElement(NS +编译,
        新XAttribute(包含@SerializedData \ Tables.xml)
    )
);

xDoc.Save(文件名);
 
xmlspy使用必备的技巧

要删除您指定的行,试试这个:

 的XNamespace NS =htt​​p://schemas.microsoft.com/developer/msbuild/2003;
XDOC的XDocument = XDocument.Load(文件名);

变种B = xDoc.Descendants(NS +编译)
    。凡(EL => el.Attribute(包含)值== @SerializedData \ Tables.xml);

如果(B!= NULL)
{
    b.Remove();
    xDoc.Save(文件名);
}
 

I am trying to add and delete elements from a C# .csproj file. The file, in part, appears below. Can someone show me how I can do the following two things?

Add an element as shown below (the line that says, "I want to add this") Delete an element. For example, say I wanted to delete the line I have indicated below.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
      <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
</PropertyGroup>
<ItemGroup>
   <Reference Include="System.Data" />    
   <Reference Include="System.Deployment" />
</ItemGroup>
<ItemGroup>
   <Compile Include="Generate\DatabaseContext.cs" />
   <Compile Include="Generate\EntityClasses.cs" />
   <Compile Include="Generate\Extensions.cs" />
   <Compile Include="Schema\Column.cs" />
   <Compile Include="Schema\EntityRef.cs" />
   <Compile Include="SerializedData\Tables.xml" />  //I want to add this
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

解决方案

You can add your indicated line in the following way:

XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument xDoc = XDocument.Load(fileName);

var b = xDoc.Descendants(ns + "Compile").First();

b.Parent.Add(
    new XElement(ns + "Compile", 
        new XAttribute("Include", @"SerializedData\Tables.xml")
    )
);

xDoc.Save(fileName);

To remove your indicated line, try this:

XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument xDoc = XDocument.Load(fileName);

var b = xDoc.Descendants(ns + "Compile")
    .Where(el => el.Attribute("Include").Value == @"SerializedData\Tables.xml");

if (b != null)
{
    b.Remove();
    xDoc.Save(fileName);
}