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

Time:09-17

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

}

CodePudding user response:

The XmlDocument instance has a xml output from LinqPad

CodePudding user response:

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