S
S
SnakeAce2014-05-17 16:05:14
Windows
SnakeAce, 2014-05-17 16:05:14

Windows 8.1 App C# - How to get data from an Xml document and write it to arrays?

There is a similar XML code:

<response list="true">
    <count>12</count>
    <post>
    <id>307</id>
    <from_id>123</from_id>
    <to_id>123</to_id>
    <date>123892128</date>
    <post_type>post</post_type>
    <text>Smth TExt</text>
    <attachments list="true">
    <attachment>
    <type>photo</type>
    <photo>
    <pid>123</pid>
    <aid>-7</aid>
    <owner_id>123</owner_id>
    <src>
    http://url.jpg
    </src>
    <src_big>
    http://url.jpg
    </src_big>
    <src_small>
    http://url.jpg
    </src_small>
    <src_xbig>
    http://url.jpg
    </src_xbig>
    <src_xxbig>
    http://url.jpg
    </src_xxbig>
    <src_xxxbig>
    http://url.jpg
    </src_xxxbig>
    <width>990</width>
    <height>1188</height>
    <text/>
    <created>135</created>
    <access_key>67</access_key>
    </photo>
    </attachment>
    <attachment>...</attachment>
    <attachment>...</attachment>
    <attachment>...</attachment>
    <attachment>...</attachment>
    </attachments>
    <comments>
    <count>0</count>
    </comments>
    <likes>
    <count>2</count>
    </likes>
    <reposts>
    <count>0</count>
    </reposts>
    </post>
    <post>...</post>
    <post>...</post>
    </response>

You need to get the value from each element in the post and write them into similar arrays, where the variable will indicate the order of the posts.
int[] int_from_id;
     int[] comments_count;
     int[] owner_id;
     string[,] srcofxxxbig; //where [№ post, № of att]
     int[] commentscount;

Actually that and a question - how to make it?
Tried like this:
private void name(){
       XDocument parsedoc = XDocument.Parse(herelinktoxmldoc);
       foreach (XElement i in parsedoc.Root.Elements())
      {
         if (w==0)
         {
             count =  Convert.ToInt32(i.Value);
         }
         w++;
     }
     count += 0;
     string[] post_from_id = new String[count];
     string[] post_to_id = new String[count];
     string[] post_date = new String[count];
     string[] post_text = new String[count];
     for (int i = 0; i < count; i++)
     {

         post_from_id[i] = ParsingXmlwoReq(wallxmlstring, "from_id");
         post_to_id[i] = ParsingXmlwoReq(wallxmlstring, "to_id");
         post_date[i] = ParsingXmlwoReq(wallxmlstring, "date");
         post_text[i] = ParsingXmlwoReq(wallxmlstring, "text");

     }
     }
     }
    private string ParsingXmlwoReq(string request, string whatget)
    {
        XDocument parsedoc = XDocument.Parse(request);
        string forreturn = "Null";
        var get = (from uri in parsedoc.Descendants(whatget) select uri.Value);
        foreach (var element in get)
        {
            forreturn = System.Convert.ToString(element);

        }
        return forreturn;
     }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SnakeAce, 2014-05-19
@SnakeAce

Used below method under Linq
private void ReadDoc()
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString); // your test XML (hard coded below)
XmlNodeList postList = xml.SelectNodes("/response/post");
Console.WriteLine(postList.Count);
foreach (XmlNode post in postList)
{
Console.WriteLine(post["from_id"]);
Console.WriteLine(post["to_id"]);
Console.WriteLine(post["date"]);
Console.WriteLine(post["text"]);
}
}

A
Alexey Kulakov, 2014-05-19
@carbon88

maybe you should use objects? everything here is perfect for this. and then somehow clumsily turns out. Well, it's probably worth looking at the mechanisms of serialization/desiralization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question