1

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

2
  • Please post the full code. It seems you are creating a node with attributes, but you might not be appending it at all. See this example on how to create nodes/attributes and saving the xml file to the disc - devx.com/tips/Tip/21168 Commented Oct 11, 2009 at 17:11
  • how do you save your XmlDocument to disk? Can you wrap that call in a try....catch block? Does an exception happen by any chance? Something like "AccessDeniedException" or something? Are you saving the file to a network drive / share ?? Commented Oct 11, 2009 at 17:27

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

I am assuming that you are trying to put backslash for the node named "path". You can't do that.

Use CDATA section to put characters that should be ignored by XML parser.

EDIT: It seems "\" is not a reserved character and I was able to edit an existing XML file & put it as below.

And the browser renders it as expected.

<Employees xmlns="http://Employees">
  <Employee id="12615" title="Architect">
    <!--This is a comment-->
    <Name>
      <First>Nancy</First>
      <Middle>J.</Middle>
      <Last>Davolio</Last>
    </Name>
    <Street>507 - 20th Ave. E. Apt. 2A</Street>
    <City>Seattle</City>
    <Zip>98122</Zip>
    <Country>
      <Name test="\abc">U.S.A.\\\\</Name>
    </Country>
    <Office>5/7682</Office>
    <Phone>(206) 555-9857</Phone>
    <Photo>Photo.jpg</Photo>
  </Employee>
</Employees>

What is the content of the variable that has backslash in it?

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.