我使用下面的代碼來解析我的XML文件:VB.NET讀取XML文件的煩惱
Dim xml As String = "<?xml version=""1.0"" encoding=""Windows-1252""?>" & _
"<theref:theref-msg xmlns:csr=""http://www.xxxxx.com/Schema/csr"" xmlns:theref=""http://www.xxxxx.com/Schema/theref"">" & _
"<theref:header>" & _
"<theref:eid />" & _
"<theref:reference_id>429</theref:reference_id>" & _
"<theref:sr_type_code>US1</theref:sr_type_code>" & _
"<theref:event_type_code>REQUEST</theref:event_type_code>" & _
"<theref:eai_event_code>DSR</theref:eai_event_code>" & _
"<theref:source_code>WORKS</theref:source_code>" & _
"<theref:target_code>APP</theref:target_code>" & _
"<theref:status_code />" & _
"<theref:details />" & _
"</theref:header>" & _
"</theref:theref-msg>"
Dim document As XDocument = XDocument.Parse(xml)
Dim pupils = From pupil In document.Descendants("theref:theref-msg") _
Select New With _
{ _
.Name = pupil.Element("theref:reference_id").Value, _
.TagID = pupil.Element("theref:sr_type_code").Value _
}
For Each pupil In pupils
Debug.Print("{0}: {1}", pupil.Name, pupil.TagID)
Next
問題的存在是,它似乎並沒有在所有的工作。它崩潰就行了:
Dim pupils = From pupil In document.Descendants("csreai:csreai-msg") _
Select New With _
{ _
.Name = pupil.Element("csreai:reference_id").Value, _
.TagID = pupil.Element("csreai:sr_type_code").Value _
}
錯誤是:型「System.Xml.XmlException」的第一個機會異常system.xml.dll的發生
類型的第一次機會異常「 System.Xml.XmlException'發生在System.Xml.dll中 ':'字符,十六進制值0x3A,不能包含在名稱中。 5
更新的代碼:
Dim xml As String = "<?xml version=""1.0"" encoding=""Windows-1252""?>" & _
"<theref:theref-msg xmlns:csr=""http://www.xxxxx.com/Schema/csr"" xmlns:theref=""http://www.xxxxx.com/Schema/theref"">" & _
"<theref:header>" & _
"<theref:eid />" & _
"<theref:reference_id>429</theref:reference_id>" & _
"<theref:sr_type_code>US1</theref:sr_type_code>" & _
"<theref:event_type_code>REQUEST</theref:event_type_code>" & _
"<theref:eai_event_code>DSR</theref:eai_event_code>" & _
"<theref:source_code>WORKS</theref:source_code>" & _
"<theref:target_code>APP</theref:target_code>" & _
"<theref:status_code />" & _
"<theref:details />" & _
"</theref:header>" & _
"<theref:body>" & _
"<csr:document>" & _
"<csr:header>" & _
"<csr:system>CSR</csr:system>" & _
"<csr:doc_name>FULLSR</csr:doc_name>" & _
"<csr:version>3.1</csr:version>" & _
"<csr:dml_event>UPDATE</csr:dml_event>" & _
"</csr:header>" & _
"</csr:document></theref:body></theref:theref-msg>"
Dim xmlb = From getXMLData In document.<theref:theref-msg>.<theref:header>.<theref:body>.<csr:document>.<csr:header>
最新更新
如果我有這樣的:
<csr:custom_attributes>
<csr:custom_attribute>
<csr:type_code>
<csr:value>data1</csr:value>
</csr:type_code>
<csr:group_code>
<csr:value>wide1</csr:value>
</csr:group_code>
</csr:custom_attribute>
<csr:custom_attribute>
<csr:type_code>
<csr:value>data2</csr:value>
</csr:type_code>
<csr:group_code>
<csr:value>wide2</csr:value>
</csr:group_code>
</csr:custom_attribute>
</csr:custom_attributes>
我只能似乎得到的第一組數據(data1,wide1)但不是s的Econd?
xmlDATA = (From getXMLData In document.<theref:csreai-msg>.<theref:body>.<csr:document>.<csr:service_request>.<csr:custom_attributes>.<csr:custom_attribute>).ToList()
VB.NET具有內嵌XML常量。爲什麼使用'String'? – 2011-04-12 19:31:57
另外,你是什麼意思「它崩潰」?請發佈例外。 – 2011-04-12 19:32:20
也看看''。這是壞的。此外,包括「編碼」聲明在這一點上是沒有意義的。 –
Tomalak
2011-04-12 19:34:09