开发者

How to read all the elements and child nodes of XML in a list using XDocument?

开发者 https://www.devze.com 2023-03-18 04:09 出处:网络
I have to get all the Entity Source,Entity Target,Property Sourceand Property Targetvalues in list respectively.

I have to get all the Entity Source,Entity Target,Property Source and Property Target values in list respectively.

<?xml version="1.0" encoding="utf-8" ?>
<Entities>
  <Entity Source="E_cdclient" Target="cd_client">
    <Property Source="KnowledgeItemId" Target="CLIENT_CONTACT_ID"/>
    <Property Source="KnowledgeClientID" Target="CLIENT_CONTACT_ID"/>
  </Entity>
  <Entity Source="E_cdclientsystem" Target="cd_client_system">
    <Property Source="PrimaryKnowledgeItemId" Target="0"/>
    <Property Source="RelatedKnowledgeId" Target="0"/>
  </Entity>
  <Entity Source="E_cdclient_cdclientcontact" Target="cd_client_contact">
    <Property Source="shortdescription" Target="analysis_short_description"/>
    <Property Source="OWNERID" Target="REF_PROJECT_OWNER_ID"/>
  </Entity>
</Entities>

I am using XDocument.

Public Function ReadXML() As List(Of String)

    'Create the XML Document'
    Dim m_xmld = New XmlDocument()

    'Load the Xml file'
    m_xmld.Load("C:\\MappingFile.xml")

    'Get the list of name nodes'
    Dim m_nodelist = m_xmld.SelectNodes("/Entities/Ent开发者_JAVA百科ity")

    Return list
End Function

How can I do this?

How to read all the elements and child nodes of XML in a list using XDocument?


    Dim lElements = (From el In xml.Descendants("Entity")
                    Select Prop1 = el.Attribute("Prop1").Value, el.Value).ToList

This code will give you a List of objects with 2 properties :

  • Prop1 : name of your attribute
  • Value : Entity node value

You can easily extend that with all the attributes you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消