Fadi
Fadi

Reputation: 191

XML TO linq get list of element into ArrayList

I have this XML file settings , How can I (USING LINQ) return the list of (StartItem) to ArrayList

and return( PriceItem) to another ArrayList

<Settings>
    <ConSetting SettingID="1">
         <Company CompanyID="1" CompanyName="CA" Code="*100#" Pin="11111" MobileName="M1">
            <StartItems StartID="1"> 094</StartItems>
            <StartItems StartID="2"> 095</StartItems>
            <StartItems StartID="4"> 097</StartItems>
            <StartItems StartID="5"> 098</StartItems>
        </Company>
        <Company CompanyID="2" CompanyName="CB" Code="*200#" Pin="22222" MobileName="M2">
            <StartItems StartID="1"> 099</StartItems>
            <StartItems StartID="2"> 093</StartItems>
            <StartItems StartID="3"> 091</StartItems>
            <StartItems StartID="4"> 092</StartItems>

        </Company>
    </ConSetting>
    <Price SettingID ="2" CompanyName="CA" >
             <Company CompanyID="1">
            <PriceItem P="40"> 50</PriceItem>
            <PriceItem P="90"> 100</PriceItem>
            <PriceItem P="200"> 225</PriceItem>

        </Company>
        <Company CompanyID="2" CompanyName="CB" >
            <PriceItem P="40"> 60</PriceItem>
            <PriceItem P="90"> 110</PriceItem>
            <PriceItem P="200"> 235</PriceItem>

        </Company>
    </Price>

</Settings>

Upvotes: 0

Views: 1449

Answers (2)

Batuu
Batuu

Reputation: 595

XDocument is easy to use in your case:

var doc = XDocument.Load("settings.xml");
var result = from items in doc.Descendants("StartItems")
             where items.Parent.Attribute("CompanyID").Value == "1"
             select new StartItem() 
             {
                 StartID = items.Attribute("StartID").Value,
                 Value = items.Value
             };
var Company1List = new ArrayList();

foreach(var item in result)
{
    Company1List.Add(item);
}

public class StartItem
{
    public string StartID { get; set; }
    public string Value { get; set; }
}

Upvotes: 2

Dr. Paul Jarvis
Dr. Paul Jarvis

Reputation: 256

You might want to look at the XmlReader Class in .NET

It should easily be able to parse this XML and then you can select certain items from the nodes into any list you want.

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx

Look at this question and answer

Reading Xml with XmlReader in C#

Upvotes: 0

Related Questions