vendredi 27 mars 2015

Parsing xml creating a dataset

Based on this .xml i try to get the photos from every article:



<?xml version="1.0" encoding="UTF-8"?>
<articles>
<article hint="0">
<id>498940</id>
<type>1</type>
<category>International</category>
<title>
<![CDATA[News title 1]]>
</title>
<description>
<![CDATA[News Description 2]]>
</description>
<content>
<photos is3idfp="CMS_12_498940" is3fechapub="2015-03-26 15:53:54">
<photo>
<photoURL>http://ift.tt/1BP5PPW;
<photodescription>
<![CDATA[U.S. Airstrikes on ISIS in Tikrit Prompt Boycott by Shiite Fighters]]>
</photodescription>
</photo>
<photo>
<photoURL>http://ift.tt/1xjinUn;
<photodescription>
<![CDATA[Challenges Weigh Heavily on Recovery Efforts in Germanwings Crash]]>
</photodescription>
</photo>
<photo>
<photoURL>http://ift.tt/1BP5OLP;
<photodescription>
<![CDATA[Saudi Arabia Leads Air Assault in Yemen]]>
</photodescription>
</photo>
</photos>
</content>
</article>
<article hint="0">
<id>498941</id>
<type>5</type>
<title>
<![CDATA[Advertisement]]>
</title>
<urlAd>http://ift.tt/1ChGphw;
</article>
<article hint="0">
<id>498940</id>
<type>1</type>
<category>International</category>
<title>
<![CDATA[News title 2]]>
</title>
<description>
<![CDATA[News Description 2]]>
</description>
<content>
<photos is3idfp="CMS_12_498940" is3fechapub="2015-03-26 15:53:54">
<photo>
<photoURL>http://ift.tt/1BP5OLR;
<photodescription>
<![CDATA[Wisconsin Guard Carries an N.B.A. Pedigree, but Is Inspired by His Mother]]>
</photodescription>
</photo>
<photo>
<photoURL>http://ift.tt/1xjinUp;
<photodescription>
<![CDATA[Kevin Love Shows What He Can Do as Cavaliers See What They Can Be]]>
</photodescription>
</photo>
<photo>
<photoURL>http://ift.tt/1xjipLW;
<photodescription>
<![CDATA[Knicks Approach a Franchise Record With a Pounding From the Clippers]]>
</photodescription>
</photo>
</photos>
</content>
</article>
</articles>


getting a dataset from the xml:



string sourceXML = "http://mydomain/myxmlfile.xml";
XmlReader xmlFile = XmlReader.Create(sourceXML, new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);


i get four tables:



ds.Tables[0].TableName, article
ds.Tables[1].TableName, content
ds.Tables[2].TableName, photos
ds.Tables[3].TableName, photo


so i try to parse and i get the articles but all the pictures:



1 title: News title 1
1 title: News Description 2
http://ift.tt/1xjipLY
http://ift.tt/1BP5OLT
http://ift.tt/1Iv9GaR
http://ift.tt/1xjipM0
http://ift.tt/19oxY9L
http://ift.tt/1BP5OLX
1 title: News title 2
1 title: News Description 2
http://ift.tt/1xjipLY
http://ift.tt/1BP5OLT
http://ift.tt/1Iv9GaR
http://ift.tt/1xjipM0
http://ift.tt/19oxY9L
http://ift.tt/1BP5OLX


I´d like to obtain only the photos for every article, this is what i've tried:



foreach (DataRow row in ds.Tables[0].Rows)
{
try
{
string element = row["type"].ToString();
article = new Article();
feed += element + " title: " + row["title"] + Environment.NewLine;
feed += element + " title: " + row["description"] + Environment.NewLine;
article.setTitle("<![CDATA[" + row["title"].ToString() + "]]>");
foreach (DataRow row1 in ds.Tables[3].Rows)
{
feed += row1["photoURL"] + Environment.NewLine;
}
listArt.Add(article);

i++;
}
catch (IndexOutOfRangeException ioe)
{
feed += "Error al crear " + sourceXML + Environment.NewLine;
feed += ioe.ToString() + Environment.NewLine;
}
}
textBox1.AppendText(feed);

Aucun commentaire:

Enregistrer un commentaire