加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – .csproj中的文件排序顺序

发布时间:2020-12-16 08:03:24 所属栏目:百科 来源:网络整理
导读:在Visual Studio csproj中,要编译的文件被引用为: ItemGroup Compile Include="CSomething.cs" Compile Include="BSomethingElse.cs" Compile Include="AYetSomethingElse.cs" ItemGroup 在我看来,订单是随机的(至少我看不到订购原则)。 在修复合并冲
在Visual Studio csproj中,要编译的文件被引用为:
<ItemGroup>
    <Compile Include="CSomething.cs"> 
    <Compile Include="BSomethingElse.cs"> 
    <Compile Include="AYetSomethingElse.cs"> 
<ItemGroup>

在我看来,订单是随机的(至少我看不到订购原则)。

在修复合并冲突时发生了几次,我错误地添加了一个文件两次(因为有很多文件,合并冲突行中的文件已经在列表中的另一个位置)。如果有一种方法来按字母顺序对Compile Included文件进行排序,这将很容易避免。

这是否可能已经(或者我必须自己写一个脚本)?我有什么副作用吗?

我刚刚遇到这个问题,因为我们团队的更多成员没有他们的解决方案文件提交,我们将文件独立添加到解决方案,他们迟来提交他们的解决方案文件,Team Foundation Server / Visual Studio Online完全无法获得合并权限。

有些文件被添加了两次,有的没有添加,所有的人都有不同的顺序的文件 – 合并地狱。

我有一种工作方式,无法使用以下C#在解决方案中排序内容文件,我在令人敬畏的LinqPad中执行。

var documentsLocation = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

var unsortedFilePath = Path.Combine(documentsLocation.ToString(),"UnsortedConfigItems.xml");

var unsortedFile = new FileInfo(unsortedFilePath);

if (!unsortedFile.Exists) {
    throw new FileNotFoundException(string.Format("The unsorted file could not be found at path {0}",unsortedFilePath));
}

var sortedFilePath = Path.Combine(documentsLocation,"SortedConfigItems.xml");

const string RootNodeName = "ItemGroup";

const string IncludeAttributeName = "Include";

var contentElementNames = new [] { "Compile","Content","None" };

var xmlFile = XDocument.Load(unsortedFile.FullName);

if (xmlFile == null || xmlFile.Root == null || xmlFile.Root.Name != RootNodeName) 
{
    Console.WriteLine("Invalid file or unexpected file schema");
    return;
}

var allElements = xmlFile.Root.Elements();

var contentElements = new List<XElement>();

foreach(var elementName in contentElementNames) {
    contentElements.AddRange(xmlFile.Root.Elements(elementName));
}

var elementsWithInclude = contentElements.Where(ce => ce.Attribute(IncludeAttributeName) != null && !string.IsNullOrWhiteSpace(ce.Attribute(IncludeAttributeName).Value));

if (!elementsWithInclude.Any())
{
    Console.WriteLine("No content elements to sort");
    return;
}

contentElements = null; // Make candidate for garbage collection

var uniqueElements = new List<XElement>(elementsWithInclude.Count());

foreach (var element in elementsWithInclude)
{
    if (!uniqueElements.Any(e => e.Attribute(IncludeAttributeName).Value == element.Attribute(IncludeAttributeName).Value)) 
    {
        uniqueElements.Add(element);
    }
}

var sortedUniqueElements = uniqueElements.OrderBy(ue => ue.Name.LocalName).ThenBy(ue => ue.Attribute(IncludeAttributeName).Value).ToList();

var remainingElements = new List<XElement>();

foreach (var element in allElements.Where(ae => !elementsWithInclude.Contains(ae))) { // This uses elements with include and not sorted unique to avoid re-adding filtered files
    remainingElements.Add(element);
}

var sortedFile = new XDocument();
sortedFile.AddFirst(new XElement(RootNodeName));

sortedUniqueElements.ForEach(sue => sortedFile.Root.Add(sue));

remainingElements.ForEach(re => sortedFile.Root.Add(re));

sortedFile.Save(sortedFilePath);

我确定它可以被稍微压缩,变得更加优雅,但它为我们做了工作。

步骤完整性 –

>确保您的项目文件在源代码管理或本地备份。>右键单击解决方案资源管理器中的项目,然后选择“卸载项目”(如果项目文件未显示,并且解决方案中只有一个项目,则在Visual Studio选项中选择“始终显示解决方案”)。>项目图标应该已更改,并将列为“(不可用)”。再次右键单击并选择“编辑ProjectName.csproj”找到包含内容引用的ItemGroup节点,并剪切或复制并粘贴到一个新文件中(NotePad是理想的,但使用您选择的编辑器)并保存在XmlPathName的位置。确保“ItemGroup”节点是新XML文档的根节点。>如果需要,更改SortedPathName中的路径,并确保您的Windows用户帐户对该位置具有写入权限。>在LinqPad中运行上面的C#(或分支,并创建自己的控制台应用程序/ Powershell脚本)。>检查输出文件的内容,以确保它看起来正确(一致的行数和格式),如果可能,验证为有效的XML(记事本中的XML工具是理想的)。>将输出文件的内容复制回您的解决方案文件,替换原始的ItemGroup节点。>关闭csproj文件,右键单击解决方案资源管理器中的项目,然后选择“重新加载项目”。>如果项目是您的启动项目,则可能需要再次右键单击并选择“设为启动项目”>尝试构建

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读