動態我試圖通過C#代碼創建xml文件,但得到以下異常:XMLException未處理'/'是一個意外的標記。預期的標記是'='。行4,位置18
XMLException未處理'/'是一個意外的標記。預期的標記是'='。 4號線,18位
這裏是我的代碼:
string docAuthor = document.Info.Author.ToString();
string docCreationDate = document.Info.CreationDate.ToString();
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("root");
xmlTextWriter.WriteStartElement("information");
xmlTextWriter.WriteElementString("Author Name", docAuthor.ToString());
xmlTextWriter.WriteElementString("Creation Date", docCreationDate.ToString());
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
XmlDocument docSave = new XmlDocument();
docSave.LoadXml(stringwriter.ToString());
//write the path where you want to save the Xml file
docSave.Save(@"C:\Information.xml");
僅供參考,您不應該使用'new XmlTextReader()'或'new XmlTextWriter()'。自.NET 2.0以來,它們已被棄用。改爲使用'XmlReader.Create()'或'XmlWriter.Create()'。 –