2014-05-20 89 views
1

Im新增了lionq到XML並且有點生疏。我想創建以下XML並將其保存爲HTMLXML元素未創建

<?xml version="1.0" encoding="UTF-8"?> 
<customers> 
<start>today</start> 
<end>next week</end> 
<Date>Some date</Date> 
<customer> 
.... 
..... 
</customer> 
</customers> 

到目前爲止,我的代碼是

Dim XD As XDocument = <?xml version="1.0" encoding="UTF-8"><Customers></Customers> 

For each c in mycustomers 
MyXDocument.Root.Add(<Customer>.....</Customer> 
Next 

它獲取數據,然後根據需要創建XML並將其保存 - 這一切作品罰款,但我不能解決如何獲得這些行

<start>today</start> 
<end>next week</end> 
<Date>Some date</Date> 

要包含在XML與我的代碼。我似乎正在將結束標記混淆,或者它只是沒有發生。任何人都可以建議嗎?

+0

我不明白問題出在哪裏。你能否以更好的方式解釋它? – faby

回答

1

只需將元素添加到您的XML文本:

Dim XD As XDocument = <?xml version="1.0" encoding="UTF-8"?> 
         <Customers> 
          <start>today</start> 
          <end>next week</end> 
          <Date>Some date</Date> 
         </Customers> 

注意:您在XML文字的XML聲明中丟失了?


當運行

For each c in Enumerable.Range(0, 5) 
    XD.Root.Add(<Customer><%= c %></Customer>) 
Next 

的XML看起來像

<Customers> 
    <start>today</start> 
    <end>next week</end> 
    <Date>Some date</Date> 
    <Customer>0</Customer> 
    <Customer>1</Customer> 
    <Customer>2</Customer> 
    <Customer>3</Customer> 
    <Customer>4</Customer> 
</Customers> 

還要注意,首先使用XD後來MyXDocument

0

您可以通過以下方式使用一個XmlTextWriter:

Dim xwriter As New XmlTextWriter(XmlFilePath, Encoding.UTF8) 
     xwriter.WriteStartDocument() 
     xwriter.WriteStartElement("customers") 

     xwriter.WriteStartElement("start") 
     xwriter.WriteString("today") 
     xwriter.WriteEndElement() 

     xwriter.WriteStartElement("end") 
     xwriter.WriteString("next week") 
     xwriter.WriteEndElement() 

     xwriter.WriteStartElement("date") 
     xwriter.WriteString("some date") 
     xwriter.WriteEndElement() 

     xwriter.WriteEndElement() 
     xwriter.WriteEndDocument() 
     xwriter.Close() 

沒有測試這一點 - 但你的想法