【发布时间】:2016-03-31 12:00:54
【问题描述】:
我对解析 xml 很陌生。我尝试了为类似问题提供的解决方案,但我拥有的 xml 格式不同。我有一个来自外部工具的 xml 文件,如下所示。我从该工具得到这个作为网络响应。
<Entities TotalResults="2">
<Entity Type="release">
<Fields>
<Field Name="name">
<Value>Release1</Value>
</Field>
<Field Name="end-date">
<Value>2015-05-15</Value>
</Field>
<Field Name="req-count">
<Value>29</Value>
</Field>
<Field Name="usedPlans">
<Value>Plan1</Value>
<Value>Plan2</Value>
</Field>
</Fields>
<RelatedEntities />
</Entity>
<Entity Type="release">
<Fields>
<Field Name="name">
<Value>Release2</Value>
</Field>
<Field Name="end-date">
<Value>2015-05-15</Value>
</Field>
<Field Name="req-count">
<Value>10</Value>
</Field>
<Field Name="usedPlans">
<Value>Plan5</Value>
<Value>Plan6</Value>
</Field>
</Fields>
<RelatedEntities />
</Entity>
</Entities>
我需要获取以下详细信息 发布名称、结束日期、请求计数、UsedPlans 等
第一个版本的示例输出为
发布名称:Release1 结束日期:2015-05-15 请求数:29 使用的计划:Plan1、Plan2VB.NET 代码:
Public Function getReleaseInfo(cookie As String, domain As String, project As String) As RestResponse
Dim resp As New relResponse()
Try
Dim relStartDate As String = ConfigurationManager.AppSettings("RelStartDate").Trim()
Dim url = (Convert.ToString((Convert.ToString((Convert.ToString(RestUrl + "/rest/domains/") & domain) + "/projects/") & project) + "/releases?query={start-date[>='") & relStartDate) + "'] }"
Dim request As WebRequest = WebRequest.Create(url)
request.Headers.Set(HttpRequestHeader.Cookie, cookie)
Dim result As WebResponse = request.GetResponse()
Dim responseStream = result.GetResponseStream()
If responseStream IsNot Nothing Then
Dim reader = New StreamReader(responseStream)
Dim output As String = reader.ReadToEnd()
Dim xml = XElement.Parse(output)
'below part gives me req-count but is there a better way.
'If I do as below, I have to do this for all items that I need
Dim reqCount = From r In releaseXML...<Field> Where r@.FirstAttribute = "req-count" Select counReq = r.Value
For Each req In reqCount
Console.WriteLine(req)
Next
End If
Catch ex As Exception
resp.Good= False
resp.Error= ex
End Try
Return resp
End Function
Dim reqCount = From r In releaseXML...<Field> Where r@.FirstAttribute = "req-count" Select counReq = r.Value
For Each req In reqCount
Console.WriteLine(req)
Next
在控制台中输出为
29
10
有没有更好的方法来做到这一点?
我有以下代码来使用 C# 执行 linq 查询,它工作正常。不幸的是,我无法将其转换为 vb.net 我尝试了许多转换器,但他们没有给我正确的 vb.net 版本的查询。我不擅长 linq 来纠正它。谁能帮我转换一下代码。
工作 C# 代码:
string output = reader.ReadToEnd();
var xml = XElement.Parse(output);
var entities = xml.Descendants("Entity");
var releases = (
from entity in entities
select entity.Descendants("Field")
into fields
select fields as IList<XElement> ?? fields.ToList()
into xElements
let name = xElements.Single(x => x.Attribute("Name").Value == "name").Value
let endDate = xElements.Single(x => x.Attribute("Name").Value == "end-date").Value
let reqCount = xElements.Single(x => x.Attribute("Name").Value == "req-count").Value
let PlansUsed = xElements.Single(x => x.Attribute("Name").Value == "usedPlans").Value
select new Release { Name = name, EndDate = endDate, ReqCount = reqCount, PlansUsed = usedPlans }).ToList();
编辑:为 linq 查询添加了工作 C# 代码。有人可以将其转换为 vb.net。注意:在线转换器和其他工具无法提供有效的转换代码。
【问题讨论】:
-
你读过documentation吗?请注意,页面底部有示例。
-
我尝试了一些东西,请参阅编辑后的代码。但我对此并不满意。我认为这不是正确的做法。
-
好吧,从
.Descendants调用中,您会得到XElements的枚举值。循环遍历它们并获取您需要的值怎么样? -
对不起,我没听懂。你能给我一个示例代码吗?
-
对不起,我对 VB.NET 中的 LINQ 和 XML 不太擅长。但是看看
Elements()、Element()和Attributes()- 这应该会让你走得很远。