How to do my XML Document to looks like the example in C# .NET 2.0?

Viewed 34

How to make my XML Document to looks like the following:

<?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>

If i start from this :

static class DirectoryComparer
{
public static XmlDocument Compare(string oldPath, string newPath)
{
XmlDocument xml = new XmlDocument();
// TODO: Needs to fill "xml" here
return xml;
}

}

2 Answers

The XmlDocument instance has a CreateElement method that allows you to create elements for your XmlDocument instance. The newly created Element (that is an XmlNode) can then be added to the Document Object Model by calling AppendChild. The Element has a SetAttribute methode to add attributes to the element.

Combining all that an implementation to get exactly your example XML might loo like this:

static class DirectoryComparer
{
    public static XmlDocument Compare(string oldPath, string newPath)
    {
        XmlDocument xml = new XmlDocument();
        xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
        
        XmlElement diff = xml.CreateElement("Diff");
        xml.AppendChild(diff);
        
        diff.AppendChild(DeleteFile(xml,"file0.ext"));
        diff.AppendChild(CreateFile(xml,"file1.ext"));
        diff.AppendChild(CreateDir(xml,"dir1"));
        diff.AppendChild(CreateDir(xml,@"dir1\dir2"));
        diff.AppendChild(CreateFile(xml,@"dir1\dir2\file2.ext"));
        return xml;
    }
    
    private static XmlElement DeleteFile(XmlDocument xml, string file) 
    {
        XmlElement delete = xml.CreateElement("Delete");
        delete.SetAttribute("file", file);
        return delete;
    }
    
    private static XmlElement CreateFile(XmlDocument xml, string file) 
    {
        return CreateFileOrDir(xml, "file", file);
    }
    
    private static XmlElement CreateDir(XmlDocument xml, string dir) 
    {
        return CreateFileOrDir(xml, "dir", dir);
    }
    
    private static XmlElement CreateFileOrDir(XmlDocument xml, string attribute,string value) 
    {
        XmlElement create = xml.CreateElement("Create");
        create.SetAttribute(attribute, value);
        return create;
    }
}

And when run from LinqPad the output looks like this:

xml output from LinqPad

Using Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication40
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.csv";
        static void Main(string[] args)
        {
            string ident = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Diff></Diff>";
            XDocument doc = XDocument.Parse(ident);
            XElement root = doc.Root;

            root.Add(new object[] {
                new XElement("Delete", new XAttribute("file", "file0.ext")),
                new XElement("Create", new XAttribute("file", "file1.ext")),
                new XElement("Create", new XAttribute("dir", "dir1")),
                new XElement("Create", new XAttribute("dir", "dir2")),
                new XElement("Create", new XAttribute("file", @"dir1\dir2\file2.ext"))
            });

            doc.Save(FILENAME);      
        }
    }      
}
Related