如何使用 XElement 解析使用 VB.NET 读取 xml 文件?

How to read xml file using VB.NET by using XElement parsing?

我对解析还很陌生 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>

我需要获取以下详细信息 发布名称、结束日期、请求计数、已用计划等

第一个版本的示例输出为

Release Name: Release1
End Date: 2015-05-15
Req Count: 29
Used Plans: Plan1, Plan2

VB.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。注意:在线转换器和其他工具无法提供有效的转换代码。

尝试这样的事情....这段代码从文件 'xml.xml' 中读取您的 XML 到一个字符串中,然后将其反序列化为一个对象,然后您可以轻松获取您想要的数据。 ....

编辑……新代码…………(包括您的 ALMReleases class)

Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization

Module Module1

    Sub Main()
        Dim deserializedXML As Entities
        Dim xmlString As String = File.ReadAllText("xml.xml")

        Dim bufXML As Byte() = ASCIIEncoding.UTF8.GetBytes(xmlString)
        Dim ms1 As MemoryStream = New MemoryStream(bufXML)

        Dim serializer As XmlSerializer = New XmlSerializer(GetType(Entities))

        Try
            Using reader As XmlReader = New XmlTextReader(ms1)
                deserializedXML = DirectCast(serializer.Deserialize(reader), Entities)

                Dim releaseInformation = (From e In deserializedXML.Entity Select New ALMReleases With {
                                                                         .relName = e.Fields.Field(0).Value.FirstOrDefault,
                                                                         .relEndDate = e.Fields.Field(1).Value.FirstOrDefault,
                                                                         .relReqCount = e.Fields.Field(2).Value.FirstOrDefault,
                                                                         .relUsedPlans = e.Fields.Field(3).Value.ToList
                                                                         }).ToList
                Dim i = 0
            End Using
        Catch ex As Exception

        End Try
        Console.ReadKey()

    End Sub

End Module

Public Class ALMReleases
    Friend relEndDate As String
    Friend relName As String
    Friend relReqCount As String
    Friend relUsedPlans As List(Of String)
End Class

<XmlRoot(ElementName:="Field")>
Public Class Field
    <XmlElement(ElementName:="Value")>
    Public Property Value() As List(Of String)
        Get
            Return m_Value
        End Get
        Set
            m_Value = Value
        End Set
    End Property
    Private m_Value As List(Of String)
    <XmlAttribute(AttributeName:="Name")>
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
End Class

<XmlRoot(ElementName:="Fields")>
Public Class Fields
    <XmlElement(ElementName:="Field")>
    Public Property Field() As List(Of Field)
        Get
            Return m_Field
        End Get
        Set
            m_Field = Value
        End Set
    End Property
    Private m_Field As List(Of Field)
End Class

<XmlRoot(ElementName:="Entity")>
Public Class Entity
    <XmlElement(ElementName:="Fields")>
    Public Property Fields() As Fields
        Get
            Return m_Fields
        End Get
        Set
            m_Fields = Value
        End Set
    End Property
    Private m_Fields As Fields
    <XmlElement(ElementName:="RelatedEntities")>
    Public Property RelatedEntities() As String
        Get
            Return m_RelatedEntities
        End Get
        Set
            m_RelatedEntities = Value
        End Set
    End Property
    Private m_RelatedEntities As String
    <XmlAttribute(AttributeName:="Type")>
    Public Property Type() As String
        Get
            Return m_Type
        End Get
        Set
            m_Type = Value
        End Set
    End Property
    Private m_Type As String
End Class

<XmlRoot(ElementName:="Entities")>
Public Class Entities
    <XmlElement(ElementName:="Entity")>
    Public Property Entity() As List(Of Entity)
        Get
            Return m_Entity
        End Get
        Set
            m_Entity = Value
        End Set
    End Property
    Private m_Entity As List(Of Entity)
    <XmlAttribute(AttributeName:="TotalResults")>
    Public Property TotalResults() As String
        Get
            Return m_TotalResults
        End Get
        Set
            m_TotalResults = Value
        End Set
    End Property
    Private m_TotalResults As String
End Class

使用代码....

  1. 创建一个新的 VB.NET 控制台应用程序
  2. 将上面的代码复制并粘贴到 'Module1' 文件中(替换自动生成的代码)
  3. 打开文件资源管理器并导航到“\…\bin\Debug”文件夹(应用程序 exe 也在这里编译)
  4. 新建一个文本文件并重命名'xml.xml'
  5. 在记事本中打开 xml.xml,粘贴您的 XML(与您的问题完全相同)并保存文件
  6. 返回 Visual Studio,按 F5 以 运行 代码.....

将出现一个控制台 window,显示您需要的结果

一旦您使用此代码,您就可以查看其工作方式并对其进行修改以满足您的特定需求......

谢谢大家的帮助。我设法解决了我的问题。 这是有效的代码

Public Function getReleaseInfo(cookie As String, domain As String, project As String) As RestResponse
    Dim resp As New relResponse()

    Try
        Dim url = "http://resturl"
        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)

             Dim releaseInformation = (From e As XElement In xml.Elements
                                          Let relName = e...<Field>.Single(Function(x) x.Attribute("Name").Value = "name").Value
                                          Let relEndDate = e...<Field>.Single(Function(x) x.Attribute("Name").Value = "end-date").Value
                                          Let relReqCount = e...<Field>.Single(Function(x) x.Attribute("Name").Value = "req-count").Value
                                          Let relUsedPlans = e...<Field>.Single(Function(x) x.Attribute("Name").Value = "usedPlans").Elements.ToList
                                          Select New myReleases With {.relName = relName, .relEndDate = relEndDate, .relReqCount = relReqCount, .relUsedPlans = relUsedPlans}).ToList

                resp.Good = True
                resp.result = releaseInformation

        End If
    Catch ex As Exception
        resp.Good= False
        resp.Error= ex
    End Try

    Return resp
End Function

myReleases class

Public Class myReleases
    Public relName As String
    Public relEndDate As Date
    Public relReqCount As Integer
    Public relUsedPlans As List(Of XElement)
    Public ReadOnly Property usedPlans As List(Of String)
        Get
            Dim plans As List(Of String) = (From p In RelUsedPlans Where Trim(p.Value) <> "" Select p.Value).ToList()
            Return plans                
        End Get
    End Property
End Class

Edit: Changed code to return used plans as a list of values