none
Load Xml data RRS feed

  • Question

  • I try to load xml data

    Here is XML

    <CONFIG>
        <Index>
            <I0>2</I0>
            <I1>2</I1>
            <I2>2</I2>
            <I3>2</I3>
            <I4>1</I4>
            <I5>0</I5>
            <I6>1</I6>
        </Index>
    </CONFIG>

    I try load 2222101 in List<int> a

    a[0] is 2  <I0>2</I0>

    a[6] is 1  <I6>1</I6>

    How to load into a like

    List<int>a=new List<int>();
    a= from element in configXElement.Descendants("Index")
                           where element.Attribute("I0").Value != null
                           select element;
     

    Help me fix this

    Thanks

    Tuesday, November 19, 2019 7:39 AM

All replies

  • The following is one solution. This assumes that values are always integers and present.

    XElement configXElement = XElement.Parse(@"<CONFIG>
        <Index>
            <I0>2</I0>
            <I1>2</I1>
            <I2>2</I2>
            <I3>2</I3>
            <I4>1</I4>
            <I5>0</I5>
            <I6>1</I6>
        </Index>
    </CONFIG>");
    
    List<int> a = configXElement.Descendants("Index")
                        .Descendants()
                        .Select(e => int.Parse(e.Value)).ToList();

    Tuesday, November 19, 2019 10:18 AM