我試圖在C#中根據客戶端的請求來格式化XML頁面。下面是我目前在XML創建在.NET中編寫XML
<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection strings, text file location, and report destination.-->
<AdminPaths Name="sqlConnection1" connectionString="asdf">
<TextPath>
<Key Value="Test3" xmlns="Path" />
</TextPath>
</AdminPaths>
這是我想擁有的一切:
<?xml version="1.0" encoding="utf-8"?>
<!--This is to write the connection string, text file location, and report destination.-->
<AdminPaths>
<Name="sqlConnection1" connectionString="asdf">
</AdminPaths>
<TextPath>
< Key="Path" Value="Test3">
< Key="Report" Value="Test2">
</TextPath>
這是我目前使用的代碼:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("C:\\Users\\fthompson11\\WebFile.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This is to write the connection strings, text file location, and report destination.");
writer.WriteStartElement("AdminPaths");
writer.WriteAttributeString("Name", "sqlConnection1");
writer.WriteAttributeString("connectionString", "asdf");
writer.WriteStartElement("TextPath");
writer.WriteStartElement("Key", "Path");
writer.WriteAttributeString("Value", "Test3");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
我曾嘗試使用writer.WriteEndElement();在writeAttributeString啓動之後,但我收到一個錯誤,指出「如果要編寫XML片段,請確保ConformanceLevel設置設置爲ConformanceLevel.Fragment或ConformanceLevel.Auto。」我相信這不是我想要做的?任何幫助,將不勝感激。
謝謝。
「我想吃點什麼」的模樣無效的XML ... – millimoose 2013-04-10 22:28:41
什麼是不正確的呢? – 2013-04-10 22:29:17
@BigPoppa xml應該有單根元素 – 2013-04-10 22:30:22