开发者

Dynamic select with linq-to-xml?

开发者 https://www.devze.com 2022-12-24 17:36 出处:网络
I got a XML document with a lot of data divided into category->subcategory. Like this: <?xml version=\"1.0\" encoding=\"utf-8\" ?>

I got a XML document with a lot of data divided into category->subcategory. Like this:

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <car>
        <transmission>
            <option value="1" text="Manual" />
            <option value="2" text="Automatic" />
        </transmission>
        <milage>
            <option value="2" text="0-499" />
            <option value="4" text="500-999" />
            <option value="6" text="1000-1499" />
            <option value="8" text="1500-1999" />
            <option value="10" text="2000-2499" />
        </milage>
        <fuel>
            <option value="1" text="Gasolin" />
            <option value="2" text="Diesel" />
            <option value="3" text="E95" />
   开发者_如何学编程         <option value="4" text="Hybrid" />
            <option value="5" text="Electric" />            
        </fuel>
    </car>
</data>

I am using Ajax to retrieve the data that i need from the xml document. Like this:

public string GetData(int typeOfData)
    {
        List<string> queryString = new List<string>();
        switch (typeOfData)
        {
            case 1:
                // Car->Milage
                queryString.Add("car");
                queryString.Add("milage");
                break;
            case 2:
                // Car-Fuel
                queryString.Add("car");
                queryString.Add("fuel");
                break;
        }

        // Now i need to construct a query to return the data, i have tried something like this:
        var results = from data in db.Elements("data") where (queryString => db.Elements(test)) select new { ID = data.Attribute("value").Value, Name = data.Attribute("text").Value };
    }
}

With XPath i could just simple make a string to query with, but how do i do this in linq?


You can use XPath in Linq.

public string GetData(int typeOfData)
{
    string query = null;
    switch (typeOfData) {
    case 1:
        query = "/data/car/milafe/option";
        break;
    case 2:
        query = "/data/car/fuel/option";
        break;
    }

    var results = from e in db.XPathSelectElements(query)
        select new { ID = e.Attribute("value").Value, Name = e.Attribute("text").Value };
}


You need to traverse the XML tree and get the element containing the options in the switch statement, like this:

public string GetData(int typeOfData)
{
    XElement container;
    switch (typeOfData)
    {
        case 1:
            // Car->Milage
            container = db.Root.Element("car").Element("mileage");
            break;
        case 2:
            // Car-Fuel
            container = db.Root.Element("car").Element("fuel");
            break;
        default:
            throw new ArugmentOutOfRangeException("typeOfData");
    }

    var results = container.Elements("option")
         .Select(data => new { ID = data.Attribute("value").Value, Name = data.Attribute("text").Value };
}
0

精彩评论

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