【发布时间】:2011-04-12 19:26:15
【问题描述】:
我正在使用以下代码来解析我的 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.dll 中发生了“System.Xml.XmlException”类型的第一次机会异常
在 System.Xml.dll 中发生了“System.Xml.XmlException”类型的第一次机会异常 ':' 字符,十六进制值 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) 而不是第二组?
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? -
另外,“它崩溃了”是什么意思?请发布例外情况。
-
也可以查看
</<theref:theref-msg>。那是坏了。此外,此时包含encoding声明毫无意义。 -
@John 他希望能够从文件中读取它,并且仅将字符串显示为示例
-
@Yet:他应该使用 XML Literal 作为示例,并且他会立即收到来自 VB.NET 的错误,显示他的错字。字符串几乎不应该用于 XML。
标签: xml vb.net xml-parsing xmlreader