我有一个存储过程调用,它返回字段中的一些XML。还有其他字段返回XML但只返回其中的一个元素。这个新字段将包含多个元素,需要循环以显示最终用户的字符串。
以下是将返回的XML示例:
<Superbills>
<Superbill>
<SuperBillID>1</SuperBillID>
<EventDate>2004-03-23T13:24:16</EventDate>
<ProcCodes>
<ProcCode>27096</ProcCode>
<ProcCode>76005</ProcCode>
<ProcCode>A4550</ProcCode>
<ProcCode>A4641</ProcCode>
<ProcCode>J3301</ProcCode>
</ProcCodes>
</Superbill>
<Superbill>
<SuperBillID>2</SuperBillID>
<EventDate>2004-03-23T13:24:16</EventDate>
<ProcCodes>
<ProcCode>27096</ProcCode>
<ProcCode>76005</ProcCode>
<ProcCode>A4550</ProcCode>
<ProcCode>A4641</ProcCode>
<ProcCode>J3301</ProcCode>
</ProcCodes>
</Superbill>
<Superbill>
<SuperBillID>3</SuperBillID>
<EventDate>2004-03-24T13:24:16</EventDate>
<ProcCodes>
<ProcCode>27096</ProcCode>
<ProcCode>76005</ProcCode>
<ProcCode>A4550</ProcCode>
<ProcCode>A4641</ProcCode>
<ProcCode>J3301</ProcCode>
</ProcCodes>
</Superbill>
</Superbills>
我需要能够循环遍历所有Superbill集以输出类似于以下内容的内容:
[EventDate] ([ProcCode], [ProcCode], [ProcCode], [ProcCode], [ProcCode])
[EventDate] ([ProcCode], [ProcCode], [ProcCode], [ProcCode], [ProcCode])
基本上,我需要为Superbill集中的每个ProcCode
构建一个逗号单独的字符串,并解析事件日期以为每个字符串构建一个字符串。
我已尝试浏览using XmlReader
,但这并没有为每个&#34; Superbill&#34;提供单独的行。元件。它最终只给我一起事件日期,然后ProcCode
一起给我。
感谢您的任何意见。
答案 0 :(得分:1)
我采用了Serializer方法......
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
Module Module1
Sub Main()
Dim XMLString As String = File.ReadAllText("SuperBills.xml")
With New XmlSerializer(GetType(Superbills)).Deserialize(New IO.MemoryStream(Encoding.UTF8.GetBytes(XMLString)))
For Each SuperBill As Superbill In .Superbills
Debug.Print("{0} ({1})", SuperBill.EventDate.ToShortDateString, Join(SuperBill.ProcCodes, ", "))
Next
End With
End Sub
End Module
<XmlRoot()>
Partial Public Class Superbills
<XmlElement("Superbill")>
Public Property Superbills As Superbill()
End Class
Partial Public Class Superbill
Public Property SuperBillID As Byte
Public Property EventDate As Date
<XmlArrayItem("ProcCode")>
Public Property ProcCodes As String()
End Class
这是你在找什么?
3/23/2004 (27096, 76005, A4550, A4641, J3301)
3/23/2004 (27096, 76005, A4550, A4641, J3301)
3/24/2004 (27096, 76005, A4550, A4641, J3301)
答案 1 :(得分:0)
如果您只想要EventData
和ProcCode
值,可以使用Linq To Xml
Dim doc As XDocument = XDocument.Parse(yourString)
var eventsAndCodes =
doc.Descendants("Superbill").
Select(Function(bill)
Return New With
{
.EventData = bill.Element("EventDate").Value,
.ProcCodes = bill.Descendants("ProcCode").Select(Function(code) code.Value).ToList()
}
End Function)
For Each data in eventsAndCodes
' Use values
data.EventData
data.ProcCodes
Next
您可以创建自己的匿名类型而不是匿名类型
Public Class EventInfo
Public Property EventDate As Date
Public Property ProcCodes As List(Of String)
End Class
从XElement
Public Function CreateEventInfoFromXElement(element As XElement) As EventInfo
Return New EventInfo With
{
.EventData = bill.Element("EventDate").Value,
.ProcCodes = bill.Descendants("ProcCode").
Select(Function(code) code.Value).
ToList()
}
End Function
然后使用它们
Dim doc As XDocument = XDocument.Parse(yourString)
Dim eventsData As IEnumerable(Of EventInfo) =
doc.Descendants("Superbill").Select(AddressOf CreateEventInfoFromXElement)
答案 2 :(得分:0)
我能够使用LinqToXml执行此操作:
var doc = XElement.Parse("...xml...");
var list = doc.XPathSelectElements("//EventDate")
.Select(e => DateTime.Parse(e.Value).ToShortDateString() + ", (" +
string.Join(", ", e.ElementsAfterSelf().First().Elements().Select(e2 => e2.Value)) +
")").ToList();
转换为VB:
Dim doc as XElement = XElement.Parse("...xml...")
Dim list as List(of String) = doc.XPathSelectElements("//EventDate").
Select(Function(e) DateTime.Parse(e.Value).ToShortDateString &
", (" & String.Join(", ", e.ElementsAfterSelf.First.Elements.
Select(Function(e2) e2.Value)) & ")").ToList
它不是非常漂亮,但它确实有效。它在文档中搜索所有EventDate
元素,将它们解析为日期,并输出ShortDate格式,然后它将所有元素放在下一个兄弟元素中,并用逗号连接它们的值。 确实假设ProcCode
元素将始终紧跟在EventDate
之后,如果不是{0}}则会失败。如果这不符合您的要求,我可以将其更新为更具弹性。
它以下列格式生成一个字符串列表,每个EventDate
一个字符串:
3/23/2004,(27096,76005,A4550,A4641,J3301)
3/23/2004,(27096,76005,A4550,A4641,J3301)
3/24/2004,(27096,76005,A4550,A4641,J3301)