2013-04-10 41 views
0

我試圖在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。」我相信這不是我想要做的?任何幫助,將不勝感激。

謝謝。

+5

「我想吃點什麼」的模樣無效的XML ... – millimoose 2013-04-10 22:28:41

+0

什麼是不正確的呢? – 2013-04-10 22:29:17

+0

@BigPoppa xml應該有單根元素 – 2013-04-10 22:30:22

回答

2

認爲這是你想要的XML。我猜測,因爲你說的XML是你的目標沒有正確形成。

<?xml version="1.0" encoding="utf-8"?> 
<!--This is to write the connection strings, text file location, and report destination.--> 
<AdminPaths> 
    <AdminPath Name="sqlConnection1" connectionString="asdf" /> 
    <TextPath> 
    <Text Key="Path" Value="Test3" /> 
    <Text Key="Report" Value="Test2" /> 
    </TextPath> 
</AdminPaths> 

基本上,你的既定目標XML,你試圖創建XML文檔下2層的節點,這是一個沒有沒有,除非你願意用ConformanceLevel.FragmentConformanceLevel.Auto,迫使這種行爲。

該代碼,希望以下內容:

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."); 

// the AdminPaths 
writer.WriteStartElement("AdminPaths"); 
writer.WriteStartElement("AdminPath"); 
writer.WriteAttributeString("Name", "sqlConnection1"); 
writer.WriteAttributeString("connectionString", "asdf"); 
writer.WriteEndElement(); 

// the TextPath's 
writer.WriteStartElement("TextPath"); 
writer.WriteStartElement("Text"); 
writer.WriteAttributeString("Key", "Path"); 
writer.WriteAttributeString("Value", "Test3"); 
writer.WriteEndElement(); 

writer.WriteStartElement("Text"); 
writer.WriteAttributeString("Key", "Report"); 
writer.WriteAttributeString("Value", "Test2"); 
writer.WriteEndElement(); 

writer.WriteEndElement(); // </AdminPaths> 
writer.WriteEndDocument(); 
writer.Flush(); 
writer.Close(); 
+0

再次感謝。這有助於一噸。 – 2013-04-10 22:52:37

1

這是喜歡的原因無效的XML:

  • 多個根元素,那就是周圍的所有的東西,一個<>元素。在第一個示例中,<AdminPaths>是單根元素,因爲其中包含其他所有內容。
  • 沒有名稱的節點。例如,<add=""無效 - 如果節點的名稱是add,則它不能直接具有值。方法是<add>Value</add>。否則,您必須具有以下屬性:<add key="">
  • 類似地,< Key="Path" Value="Test3">是無效的,因爲它沒有名稱,只有兩個屬性(鍵和值),也沒有關閉(節點需要有結束標記,如<add></add>或自閉樣<add />(注意>前/)

閱讀上一個節點或元素和XML的屬性,它應該成爲比較明確的,你需要建立什麼,然後之間的差異,我們可以解決的方法如下:

+0

我認爲這應該是一個評論,而不是一個解決方案 – 2013-04-10 22:36:38

+0

@lazyberezovsky這是太長的評論:/ – 2013-04-10 22:37:03

+1

同意,但從技術上說,這仍然是一個評論:) – 2013-04-10 22:38:06