开发者

i can't write to XML string containing path to file, c# Net 3.5

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


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜