tags:

views:

84

answers:

2

i got file which contains simple XML structure, to operate on xml i use classes bulit in framework 3.5, for string not containing backslashes everything works fine, but in case strings i try to write contain backslashes final file isn't saved to disk, no exception or any kind of error at all. No matter if i write it as parrameter or as value of node, i even tried replace "\" with "\\" with no succes. What I'm doing wrong?

to create node i use following code

    public XmlElement ToXml(XmlDocument docXml){
        XmlElement answer = docXml.CreateElement("datafile");
        answer.SetAttribute("name", dfName);
        answer.SetAttribute("path", dfPath);
        answer.SetAttribute("user", dfUser);
        answer.SetAttribute("pass", dfPass);
        answer.SetAttribute("defalut", isDefault.ToString().ToLower());
        return answer;
    }

Thanks in advance for any suggestions

Paul

+1  A: 
shahkalpesh
I removed my answer and voted up this one. It seems to be more related to the actual question. In addition, it seems to be correct.
L. Moser
+1  A: 

I tried this myself and I have no trouble whatsoever:

class Program
{
    static void Main(string[] args)
    {
        // get a list of files
        string[] files = Directory.GetFiles(@"D:\backup");

        // create new XML document
        XmlDocument xdoc = new XmlDocument();

        // add a "root" node
        xdoc.AppendChild(xdoc.CreateElement("ListOfFiles"));

        foreach (string file in files)
        {
            xdoc.DocumentElement.AppendChild(CreateXmlElement(xdoc, file));                
        }

        // save file
        xdoc.Save(@"D:\filelist.xml");
    }

    private static XmlElement CreateXmlElement(XmlDocument xmldoc, string filename)
    {
        XmlElement result = xmldoc.CreateElement("datafile");

        result.SetAttribute("name", Path.GetFileName(filename));
        result.SetAttribute("path", Path.GetDirectoryName(filename));
        result.SetAttribute("fullname", filename);

        return result;
    }
}

Gives me a nice, clean XML file as a result:

<ListOfFiles>
  <datafile name="mse-01-14.zip" path="D:\backup" fullname="D:\backup\mse-01-14.zip" />
  <datafile name="Vor_09.iso" path="D:\backup" fullname="D:\backup\Vor_09.iso" />
</ListOfFiles>

Not a single problem at all.

Marc

marc_s