将元素添加到 xml 文件

发布于 2024-08-16 14:35:07 字数 1098 浏览 4 评论 0原文

我正在尝试从 C# .csproj 文件添加和删除元素。该文件的部分内容如下所示。有人可以告诉我如何做以下两件事吗?

  1. 添加一个元素,如下所示(该行显示“我想添加 this")
  2. 删除一个元素。例如,假设我想删除 I 行 已在下面指出。
<?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>

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?

  1. Add an element as shown below (the line that says, "I want to add
    this")
  2. 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美胚控场 2024-08-23 14:35:07

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

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);

要删除指示行,请尝试以下操作:

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);
}

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);
}
尤怨 2024-08-23 14:35:07

我想这应该没问题

XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));

xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));

xmlDoc.Save(Server.MapPath("People.xml"));

Ithink this should be fine

XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));

xmlDoc.Element("Persons").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));

xmlDoc.Save(Server.MapPath("People.xml"));
私野 2024-08-23 14:35:07
        XDocument projects = XDocument.Load(fileName);
        XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";

        // Delete element (<Compile Include="SerializedData\Tables.xml" />);
        var query1 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup").Descendants(xmlns + "Compile") 
                     where p.Attribute("Include").Value == @"SerializedData\Tables.xml" select p;
        if (query1.Any())
        {
            XElement node = query1.Single();
            node.Remove(); 
        }

        //System.Diagnostics.Debug.WriteLine(projects);
        projects.Save(fileName);

        // Add the element.
        var query2 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup") where p.Descendants(xmlns + "Compile").Any() select p;
        if (query2.Any())
        {
            query2.Single().Add(new XElement(xmlns + "Compile", new XAttribute("Include", @"SerializedData\Tables.xml")));  
        }
        //System.Diagnostics.Debug.WriteLine(projects);
        projects.Save(fileName);
        XDocument projects = XDocument.Load(fileName);
        XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003";

        // Delete element (<Compile Include="SerializedData\Tables.xml" />);
        var query1 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup").Descendants(xmlns + "Compile") 
                     where p.Attribute("Include").Value == @"SerializedData\Tables.xml" select p;
        if (query1.Any())
        {
            XElement node = query1.Single();
            node.Remove(); 
        }

        //System.Diagnostics.Debug.WriteLine(projects);
        projects.Save(fileName);

        // Add the element.
        var query2 = from p in projects.Descendants(xmlns + "Project").Descendants(xmlns + "ItemGroup") where p.Descendants(xmlns + "Compile").Any() select p;
        if (query2.Any())
        {
            query2.Single().Add(new XElement(xmlns + "Compile", new XAttribute("Include", @"SerializedData\Tables.xml")));  
        }
        //System.Diagnostics.Debug.WriteLine(projects);
        projects.Save(fileName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文