0

Below is the XML data I am getting from a URL. When I view source, here is how it looks:

<xml>
    <beginning>

        <id>information from rss provider here</id>
        <updated>information from rss provider here</updated>
        <link rel='alternate' type='text/html' href='tttt' title='alternate'/>
        <link rel='self' type='application/atom+xml' href='tttt'/>

        <someschemas>
            <id>test</id>
            <movieid>test</movieid>
            <updated>teeeeest</updated>
            <types scheme='vals' term='test'/>
            <title type='html'>test</title>
            <summary type='html'>test</summary>
            <descriptions type='html'>test</descriptions>
            <link rel='alternate' type='text/html' href='tttt' title='alternate'/>
            <link rel='self' type='application/atom+xml' href='tttt'/>
            <actor>
                <name>teest</name>
                <phone>test</phone>
            </actor>
        </someschemas>

        <someschemas>
            <id>test</id>
            <movieid>test</movieid>
            <updated>teeeeest</updated>
            <types scheme='vals' term='test'/>
            <title type='html'>test</title>
            <summary type='html'>test</summary>
            <descriptions type='html'>test</descriptions>
            <link rel='alternate' type='text/html' href='tttt' title='alternate'/>
            <link rel='self' type='application/atom+xml' href='tttt'/>
            <actor>
                <name>teest</name>
                <phone>test</phone>
            </actor>
        </someschemas>

        <someschemas>
            <id>test</id>
            <movieid>test</movieid>
            <updated>teeeeest</updated>
            <types scheme='vals' term='test'/>
            <title type='html'>test</title>
            <summary type='html'>test</summary>
            <descriptions type='html'>test</descriptions>
            <link rel='alternate' type='text/html' href='tttt' title='alternate'/>
            <link rel='self' type='application/atom+xml' href='tttt'/>
            <actor>
                <name>teest</name>
                <phone>test</phone>
            </actor>
        </someschemas>
    </beginning>
</xml>

I am able to read the content in a message box:

WebRequest request = WebRequest.Create("http://www.test.com/file.xml");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();   
XElement xelement = XElement.Load(dataStream);                      
IEnumerable<XElement> employees = xelement.Elements();            
foreach (var employee in employees)
{
    if (employee.Elements("content") != null)
    {
        MessageBox.Show(employee.Value.ToString());
    }                 
}

I would like to save this to an array, or a list, or LINQ.

How can i use the code above with the XML above and make it into an array.

I only want all the data within <someschemas>. and just these values as key/value pairs:

<title type='html'>test</title>
    <summary type='html'>test</summary>
    <descriptions type='html'>test</descriptions>
<actor>
<name>teest</name>
<phone>test</phone>
</actor>
3
  • 1
    Well why don't you create an Employee class? You are looping through stuff already, just add it to a class, give that some semantic meaning and you are sorted. For instance, instead of showing employee.Value, add it to a List. Commented Sep 30, 2013 at 14:01
  • hi Arran, i would like the value to be in key/value pair. thanks. Commented Sep 30, 2013 at 14:19
  • Take note that WebResponse and Stream are IDisposable resources and therefore should be within using blocks. Commented Sep 30, 2013 at 14:21

2 Answers 2

4

Here is a LINQ to XML Version:

string[] names = XElement.Load(dataStream).Descendants("Name")
                        .Select(element => element.Value).ToArray();

This will give all the Name element from the document.

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

5 Comments

Good, except your call ToArray() when your variable is an XElement. Return type should be string[].
i got an error that says you cannot implicitly convert type string[] to system.xml.linq.xlelement
great thanks, so now i can loop through the 'names' and access the key/value pairs?
@Menew, for that, you need to edit your post to define what your key/value pairs are. And you can use your names in a for/foreach loop.
@Nirmal your answer was also right, however, i needed to create a detailed key/value pair.
1

For completeness, you need to make your own class to store each someschemas object.

class SomeSchemas
{
    public string ID {get;set;}
    public string MovieId {get;set;}
    //add in others here
    public string Actor_Name {get;set;}
    public string Actor_Phone {get;set}
}

Then loop through and add your items

List<SomeSchemas> items = XElement.Load(dataStream)
    .Descendants("someschemas")
    .Select(x => new SomeSchemas()
    {
        ID = x.Element("id").Value,
        MovieId = x.Element("movieid").Value,
        //add in others here
        Actor_Name = x.Element("actor").Element("name").Value,
        Actor_Phone = x.Element("actor").Element("phone").Value
    }.ToList();

If Actors can have more than one entry, then you need to make a List object inside SomeSchemas.

Then you can use items in a loop, and do linq on it. It won't be a key/value pair (like a dictionary), but you can select an item based on its ID, if they are unique.

SomeSchemas myObject = items.Single(x => x.ID == "asdf");

Comments

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.