以XML格式导航项目文件

时间:2012-02-22 17:55:44

标签: c# xml linq msbuild

我希望解析和修改.dbproj类型的项目文件。

示例XML

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    <ProjectGroup></ProjectGroup>
    <ItemGroup></ItemGroup>
    <ItemGroup></ItemGroup>
    <ItemGroup></ItemGroup>
    <ItemGroup>
      <Build Include="myfile.cs">
           <SubType>Code</SubType>
          </Build>
      <Build Include="myfile2.cs">
           <SubType>Code</SubType>
          </Build>
      <Build Include="myfile3.cs">
           <SubType>Code</SubType>
          </Build>
    </ItemGroup>
    <ItemGroup>
      <NotInBuild Include="myfile4.cs">
        <SubType>Code</SubType>
      </NotInBuild>
    </ItemGroup>
</Project>

我想从第4个<ItemGroup>获取带有Include="myfile.cs"属性的项目,并将其从所述<ItemGroup>中删除。接下来,我想添加这个元素:

<NotInBuild Include="myfile.cs">
    <SubType>Code</SubType>
</NotInBuild>

从删除的那个到{5} ItemGroup略有修改。结果应该是:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
    <ProjectGroup></ProjectGroup>
    <ItemGroup></ItemGroup>
    <ItemGroup></ItemGroup>
    <ItemGroup></ItemGroup>
    <ItemGroup>
      <Build Include="myfile2.cs">
           <SubType>Code</SubType>
          </Build>
      <Build Include="myfile3.cs">
           <SubType>Code</SubType>
          </Build>
    </ItemGroup>
    <ItemGroup>
      <NotInBuild Include="myfile.cs">
        <SubType>Code</SubType>
      </NotInBuild>
      <NotInBuild Include="myfile4.cs">
        <SubType>Code</SubType>
      </NotInBuild>
    </ItemGroup>
</Project>

Skeet的另一个SO post,我开始走LINQ路径。我不知道从哪里开始

XDocument doc = XDocument.Load(XmlDocumentPath);
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
                var query = doc.Root          
                .Elements(ns + "ItemGroup");

1 个答案:

答案 0 :(得分:1)

使用此集合类及相关扩展程序:http://searisen.com/xmllib/xelementcollection.wiki

这些课程:

public class Project
{
    XElement self;
    public Project(FileInfo file)
    {
        self = XElement.Load(file.FullName);
    }

    public ItemGroup[] ItemGroup
    {
        get
        {
            return _ItemGroup
                ?? (_ItemGroup = self.GetEnumerable("ItemGroup", x => new ItemGroup(x)).ToArray());
        }
    }
    ItemGroup[] _ItemGroup;
}

public class ItemGroup
{
    XElement self;
    public ItemGroup(XElement self)
    {
        this.self = self;
    }

    public XElementCollection Build
    {
        get
        {
            return _Build
                ?? (_Build = new XElementCollection(self, "Build",
                         (a, b) => a.Get("Include", string.Empty) ==
                                   b.Get("Include", string.Empty),
                         false));
        }
    }
    XElementCollection _Build;

    public XElementCollection NotInBuild
    {
        get
        {
            return _NotInBuild
                ?? (_NotInBuild = new XElementCollection(self, "NotInBuild",
                         (a, b) => a.Get("Include", string.Empty) ==
                                   b.Get("Include", string.Empty),
                         false));
        }
    }
    XElementCollection _NotInBuild;
}

然后是这种混乱的实现:

Project project = new Project(xmlFile2);
XElement find = null;
ItemGroup item = project.ItemGroup.FirstOrDefault(i =>
    (find = i.Build.Find(x => x.Get("Include", string.Empty) == "myfile.cs")) != null);
if (null != find)
{
    XElement not = new XElement(find.Name.Namespace + "NotInBuild");
    not.Set("Include", "myfile.cs", true);
    foreach (var child in find.Elements().ToArray())
        not.Add(child);
    find.Remove();
    ItemGroup notGroup = project.ItemGroup.FirstOrDefault(i => i.NotInBuild.Count > 0);
    notGroup.NotInBuild.Add(not);
}

使用顶部提到的扩展方法,名称空间问题不是问题。