日期:2014-05-18  浏览次数:20730 次

XML写文件问题
C# code

            string a = "1.jpg";
            string b = "第一张图";
            string c = "http://www.sina.com.cn/ETicket/Index.aspx?id=100&type=0";
            StringBuilder sb = new StringBuilder();
            sb.Append("<item item_url='../Advertisment/Image/" + a + "' itemtitle='" + b + "' link='" + c + "' />");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("<?xml version='1.0' encoding='gb2312'?>" +
                            "<bcaster>" +
                            sb.ToString() +
                            "</bcaster>");
            xmlDoc.Save(@"C:\data.xml");



这个会报错。“=”是意外的标记,标记应为“;”
请大家帮忙看看sb.ToString()是哪里出错了。

------解决方案--------------------
把&转义一下:
C# code

  string a = "1.jpg";
            string b = "第一张图";
            string c = "http://www.sina.com.cn/ETicket/Index.aspx?id=100&amp;type=0";
            StringBuilder sb = new StringBuilder();
            sb.Append("<item item_url='../Advertisment/Image/" + a + "' itemtitle='" + b + "' link='" + c + "' />");
            XmlDocument xmlDoc = new XmlDocument();
            string s = "<?xml version='1.0' encoding='gb2312'?>" +
                            "<bcaster>" +
                            sb.ToString() +
                            "</bcaster>";            
            xmlDoc.LoadXml(s);
            xmlDoc.Save(@"C:\data.xml");

------解决方案--------------------
string c = "http://www.sina.com.cn/ETicket/Index.aspx?id=100&type=0";
中含有等号,考虑一下转义