0

I have the following XML file:

<Categories>
<Category name="TopDown">
    <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
    <Path>http://localhost:8080/images/TopDown/Blu-Ray.png</Path>
    <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
    <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
    <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
    <Path>http://localhost:8080/images/TopDown/Divx.png</Path>  
</Category>
<Category name="SideScroll">
    <Path>http://localhost:8080/images/SideScroll/MediaMonkey.png</Path>
    <Path>http://localhost:8080/images/SideScroll/Miro.png</Path>
    <Path>http://localhost:8080/images/SideScroll/QuickTime.png</Path>
    <Path>http://localhost:8080/images/SideScroll/VLC.png</Path>
    <Path>http://localhost:8080/images/SideScroll/WinAmp.png</Path>
</Category>

In my c# code, I have a function that gets a string that represents the Category "name" attribute, and if that string equals to that attribute I'd like to get all the text between the "Path" tags. For Instance, if the function gets a string parameter that equals "TopDown" the output would be :

http://localhost:8080/images/TopDown/Divx.png
    http://localhost:8080/images/TopDown/Blu-Ray.png
    http://localhost:8080/images/TopDown/Divx.png
    http://localhost:8080/images/TopDown/Divx.png
    http://localhost:8080/images/TopDown/Divx.png
    http://localhost:8080/images/TopDown/Divx.png

Thank you.

1 Answer 1

1

You can do this with LINQ To XML:

var xdoc = @"<Categories>
    <Category name='TopDown'>
        <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
        <Path>http://localhost:8080/images/TopDown/Blu-Ray.png</Path>
        <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
        <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
        <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
        <Path>http://localhost:8080/images/TopDown/Divx.png</Path>
    </Category>
    <Category name='SideScroll'>
        <Path>http://localhost:8080/images/SideScroll/MediaMonkey.png</Path>
        <Path>http://localhost:8080/images/SideScroll/Miro.png</Path>
        <Path>http://localhost:8080/images/SideScroll/QuickTime.png</Path>
        <Path>http://localhost:8080/images/SideScroll/VLC.png</Path>
        <Path>http://localhost:8080/images/SideScroll/WinAmp.png</Path>
    </Category>
</Categories>";

var paths = XDocument.Parse(xdoc).Descendants("Category")
        .Where(w => (string)w.Attribute("name") == "TopDown")
        .Select(s => s.Elements("Path").Select (x => (string)x)).ToList();


foreach (var x in paths)
    Console.WriteLine(x);

You can copy paste that into linqpad or visual studio and it'll run.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much, though I don't really understand Linq and there's some problem with that code, could you please test it? it has a compile error. "An object Reference is required for the non-static field
What version of .NET are you using? XmlDocument and XDocument are 2 different things.
up the top of your code file, make sure you have: using System.Xml.Linq;
OK I added that, now there's the following error: Error 2 Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<string>>' to 'System.Collections.Generic.List<string>' C:\Users\bezeq\Documents\Visual Studio 2010\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 50 21 ConsoleApplication7
Oh I just saw that you edited your code, it is not possible to load the xml document?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.