如何读取XML以获取属性和元素数据 [英] How do I Read XML to get Attribute and Element data

查看:246
本文介绍了如何读取XML以获取属性和元素数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有以下XML字符串示例:



Hi

I have the following XML string example:

<Attributes>
<ProductVariantAttribute ID="6"><ProductVariantAttributeValue><Value>12</Value></ProductVariantAttributeValue></ProductVariantAttribute>		
<ProductVariantAttribute ID="1"><ProductVariantAttributeValue><Value>1</Value></ProductVariantAttributeValue></ProductVariantAttribute>		
<ProductVariantAttribute ID="3"><ProductVariantAttributeValue><Value>6</Value></ProductVariantAttributeValue></ProductVariantAttribute>
</Attributes>





我需要获取每个ID =数字,然后获取Value之间的数字,并在获取每个值之间做一些事情。



我能够得到Value之间的数字:





I need to get each ID= number and then the number between Value and do something in between getting each value.

I was able to get the number between Value using:

Dim xmlAtt As New XmlDocument()
xmlAtt.LoadXml(varReader("AttributesXml"))

Dim varIdCount As Integer = xmlAtt.DocumentElement.GetElementsByTagName("Value").Count
Dim y As Integer

For y = 0 To varIdCount - 1
    'varAttId = <GET ID NUMBER>
    <DO SOMETHING>
    varAttValue = xmlAtt.DocumentElement.GetElementsByTagName("Value").Item(i).InnerText
    <DO SOMETHING>
Next





但我无法弄明白同时获取属性ID值。



提前致谢!!



But I can't figure out how to get the attribute ID value at the same time.

Thanks in advance!!

推荐答案

< b> e



我不是这里最有经验的人来回答这个问题,但我会试试。



首先,我相信你的XML结构需要调查。



你期望你的价值超过1吗? ProductVariantAttributeValue元素?



如果没有,那么你可以省略Value元素并将真实值放在ProductVariantAttributeValue元素中。



否则,我将不得不调整我的代码以适应。



但是以下应该做你期望的:



eHi,

I'm not the most experienced person around here to answer this but I'll try.

First, I believe that the structure of you XML needs to looking into.

Do you expect more than 1 value in your ProductVariantAttributeValue element?

If not, then you could just leave out the Value element and put the real value in your ProductVariantAttributeValue element.

Otherwise, I'll have to adjust my code to accomodate.

But the following should do what you expect:

Imports System.Linq

Module Module1

    Sub Main()

        Dim query As IEnumerable(Of XElement) = From q In XML...<productvariantattribute> Select q

        For Each el In query
            Dim newAttr As New Attributes With {
                .ID = el.@ID,
                .Value = el...<value>.Value}

            AttributesList.Add(newAttr)
        Next

        For Each el In AttributesList
            Console.WriteLine("ID: " & el.ID)
            Console.WriteLine("Value: " & el.Value)
        Next

        Console.ReadLine()
    End Sub

    Dim XML As XDocument =
        
        <attributes>
            <productvariantattribute id="6">
                <productvariantattributevalue>
                    <value>12</value>
                </productvariantattributevalue>
            </productvariantattribute>
            <productvariantattribute id="1">
                <productvariantattributevalue>
                    <value>1</value>
                </productvariantattributevalue>
            </productvariantattribute>
            <productvariantattribute id="3">
                <productvariantattributevalue>
                    <value>6</value>
                </productvariantattributevalue>
            </productvariantattribute>
        </attributes>

    Dim AttributesList As New List(Of Attributes)
End Module

Public Class Attributes
    Property ID As String
    Property Value As String
End Class</value></productvariantattribute>





我创建了一个类来保存你的返回值,因为我相信这更符合OOP。



此外,XML读取是使用linq完成的,因为我比使用DOM元素更熟悉它。



I create a class to hold your return values as I believe this is more inline with OOP.

Also, the XML read is done with linq because I'm a bit more familiar with it than using DOM elements.


这篇关于如何读取XML以获取属性和元素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆