0

When I run <!-- This list contains names/words with specific casing - and specific to english only --> will be removed in new destination. Is there any way to prevent it from happening. I event tried with XDocument.Root same thing happened. Ps: I know method are not inside class :).

<!-- This list contains names/words with specific casing - and specific to english only -->
<ignore_list>
  <name>A.M.</name>
  <name>Aaron</name>
  <name>Abbie</name>
  <name>Abi</name>
  <name>Abigail</name>
  <name>Abolfazl</name>
  <name>Adam</name>
</ignore_list>
private static void EnGBNamesEtc()
{
        var listNames = new List<string>(){"some", "names");
        var xele = XElement.Load(@"C:\Users\ivandro\Source\subtitleedit\Dictionaries\en_GB_names_etc.xml");
        var names = listNames.Except(xele.Elements("name").Select(n => n.Value)).ToList();
        foreach (var newName in names)
        {
            xele.Add(new XElement("name", newName));
        }
        SaveToNewXml(xele, @"D:\Backup\en_GB_names_etc1.xml");
}

private static void SaveToNewXml(XElement xelem, string dest)
{
    XmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
    var ordered = xelem.Elements("name").OrderBy(element => element.Value).ToList();
    using (XmlWriter xw = XmlWriter.Create(dest, xws))
    {
        xelem.ReplaceAll(ordered);
        xelem.Save(xw);
    }
}
Ivandro Jao
  • 2,731
  • 5
  • 24
  • 23
  • This SO might help http://stackoverflow.com/questions/20481741/c-sharp-reading-xml-comment-using-xdocument – samuelesque Jun 09 '14 at 13:23
  • Not related to your question, but: 1. `async` without `await` doesn't make much sense, and you're going to get a warning for that. 2. [You should avoid `async void` methods.](http://msdn.microsoft.com/en-us/magazine/jj991977.aspx) – svick Jun 09 '14 at 15:35
  • the method is using `WebClient` to download content... I removed it to make it more simple and forgot async ;) @svick – Ivandro Jao Jun 09 '14 at 16:41

1 Answers1

2

That's possibly because you're using XElement which only capable of representing single node (when you have two "root" nodes, <ignore_list> and the comment node). Use XDocument instead of XElement to keep the comment node, for example (I tried to keep minimal changes to your original code) :

private static async void EnGBNamesEtc()
{
        var listNames = new List<string>(){"some", "names");
        var xdoc = XDocument.Load(@"C:\Users\ivandro\Source\subtitleedit\Dictionaries\en_GB_names_etc.xml");
        var names = listNames.Except(xdoc.Root.Elements("name").Select(n => n.Value)).ToList();
        foreach (var newName in names)
        {
            xdoc.Root.Add(new XElement("name", newName));
        }
        SaveToNewXml(xdoc, @"D:\Backup\en_GB_names_etc1.xml");
}

private static void SaveToNewXml(XDocument xdoc, string dest)
{
    XmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
    var ordered = xdoc.Root.Elements("name").OrderBy(element => element.Value).ToList();
    using (XmlWriter xw = XmlWriter.Create(dest, xws))
    {
        xdoc.Root.ReplaceAll(ordered);
        xdoc.Save(xw);
    }
}
har07
  • 88,338
  • 12
  • 84
  • 137