You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# .NET 2.0中生成指定格式的XML文档?

.NET 2.0下生成指定差异XML文档的实现

需求格式

需要生成如下结构的XML文档:

<?xml version="1.0" encoding="utf-8" ?>
<Diff>
<Delete file="file0.ext"/>
<Create file="file1.ext"/>
<Create dir="dir1"/>
<Create dir="dir1\dir2"/>
<Create file="dir1\dir2\file2.ext"/>
</Diff>

完整代码实现

以下是填充XmlDocument部分的完整代码,实现对比两个目录差异并生成指定XML:

static class DirectoryComparer
{
    public static XmlDocument Compare(string oldPath, string newPath)
    {
        XmlDocument xml = new XmlDocument();
        
        // 添加XML声明
        XmlDeclaration xmlDeclaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);
        xml.AppendChild(xmlDeclaration);
        
        // 创建根节点<Diff>
        XmlElement diffRoot = xml.CreateElement("Diff");
        xml.AppendChild(diffRoot);
        
        // 处理需删除的项:旧目录存在、新目录不存在的文件/文件夹
        // 遍历旧目录所有文件
        foreach (string file in Directory.GetFiles(oldPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = GetRelativePath(oldPath, file);
            string newFilePath = Path.Combine(newPath, relativePath);
            if (!File.Exists(newFilePath))
            {
                XmlElement deleteElem = xml.CreateElement("Delete");
                deleteElem.SetAttribute("file", relativePath);
                diffRoot.AppendChild(deleteElem);
            }
        }
        // 遍历旧目录所有文件夹
        foreach (string dir in Directory.GetDirectories(oldPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = GetRelativePath(oldPath, dir);
            string newDirPath = Path.Combine(newPath, relativePath);
            if (!Directory.Exists(newDirPath))
            {
                XmlElement deleteElem = xml.CreateElement("Delete");
                deleteElem.SetAttribute("dir", relativePath);
                diffRoot.AppendChild(deleteElem);
            }
        }
        
        // 处理需创建的项:新目录存在、旧目录不存在的文件/文件夹
        // 遍历新目录所有文件
        foreach (string file in Directory.GetFiles(newPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = GetRelativePath(newPath, file);
            string oldFilePath = Path.Combine(oldPath, relativePath);
            if (!File.Exists(oldFilePath))
            {
                XmlElement createElem = xml.CreateElement("Create");
                createElem.SetAttribute("file", relativePath);
                diffRoot.AppendChild(createElem);
            }
        }
        // 遍历新目录所有文件夹
        foreach (string dir in Directory.GetDirectories(newPath, "*", SearchOption.AllDirectories))
        {
            string relativePath = GetRelativePath(newPath, dir);
            string oldDirPath = Path.Combine(oldPath, relativePath);
            if (!Directory.Exists(oldDirPath))
            {
                XmlElement createElem = xml.CreateElement("Create");
                createElem.SetAttribute("dir", relativePath);
                diffRoot.AppendChild(createElem);
            }
        }
        
        return xml;
    }
    
    // .NET 2.0兼容的相对路径获取方法
    private static string GetRelativePath(string rootPath, string fullPath)
    {
        rootPath = Path.GetFullPath(rootPath);
        // 确保根路径以目录分隔符结尾,避免匹配异常
        if (!rootPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
        {
            rootPath += Path.DirectorySeparatorChar;
        }
        
        Uri rootUri = new Uri(rootPath);
        Uri fullUri = new Uri(fullPath);
        
        // 将URI相对路径转为Windows本地路径格式
        return Uri.UnescapeDataString(rootUri.MakeRelativeUri(fullUri).ToString()).Replace('/', '\\');
    }
}

代码说明

  • XML结构构建:先创建XML声明和根节点<Diff>,后续所有差异项都作为根节点的子元素添加。
  • 差异逻辑:
    • Delete节点:遍历旧目录的所有文件和文件夹,若在新目录中不存在则生成。
    • Create节点:遍历新目录的所有文件和文件夹,若在旧目录中不存在则生成。
  • 相对路径处理:因为.NET 2.0没有内置的相对路径获取方法,通过Uri类实现路径转换,并转为Windows风格的反斜杠路径。

内容的提问来源于stack exchange,提问作者Milen Ivanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:31:00