read xml with same elements name - c#

I have xml in format below:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<draw><drawNo>381555</drawNo>
<drawTime>2013-04-29T19:55:00+03:00</drawTime>
<result>8</result>
<result>10</result>
<result>13</result>
<result>15</result>
<result>20</result>
<result>21</result>
<result>22</result>
<result>25</result>
<result>28</result>
<result>29</result>
<result>34</result>
<result>36</result>
<result>44</result>
<result>46</result>
<result>52</result>
<result>62</result>
<result>63</result>
<result>72</result>
<result>73</result>
<result>75</result>
</draw>
I need to split the data...
I've tried the code below:
XDocument loadeddata = XDocument.Parse(e.Result);
var data = from query in loadeddata.Descendants("draw")
select new KinnoResults()
{
DrawNo = (String) query.Element("drawNo").Value,
DrawTime = (String) query.Element("drawTime").Value,
result1 = (String)query.Element("result").Value,
result2 = (String)query.Element("result").Value
};
List<KinnoResults> list = data.ToList();
But result1 and result2 hava the same value 8.
Any idea please?

var drawNo = loadeddata.Root.Element("drawNo").Value;
var drawTime = loadeddata.Root.Element("drawTime").Value;
var results = loadeddata.Descendants("result").Select(d => d.Value).ToList();

use Elements, it gets you a collection
select new KinnoResults()
{
DrawNo = (String)query.Elements("drawNo").Value,
DrawTime = (String)query.Element("drawTime").Value,
result1 = (String)query.Elements("result").ToList()[0].Value,
result2 = (String)query.Elements("result").ToList()[1].Value
};

Related

XmlDocument sort nodes by the inner text value of a child

i want to sort the nodes called ImageInfo by the number in the pos node because i have buttons that change the position up or down and i need to sort the ImageInfo node in the correct order when the pos has changed.
i apologise ahead for not having any c# code but i assure you that i have tried so many different things and im in need of help.
here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<MplAndSiImages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MplImages>
<ImageInfo>
<pos>1</pos>
<Name>1.png</Name>
<ParentObjectId>b66a23a8-6268-e611-80e2-c4346bad02e8</ParentObjectId>
<Url>http://localhost:8080/b66a23a8-6268-e611-80e2-c4346bad02e8/1.png</Url>
</ImageInfo>
<ImageInfo>
<pos>2</pos>
<Name>2.png</Name>
<ParentObjectId>b66a23a8-6268-e611-80e2-c4346bad02e8</ParentObjectId>
<Url>http://localhost:8080/b66a23a8-6268-e611-80e2-c4346bad02e8/2.png</Url>
</ImageInfo>
<ImageInfo>
<pos>3</pos>
<Name>3.png</Name>
<ParentObjectId>b66a23a8-6268-e611-80e2-c4346bad02e8</ParentObjectId>
<Url>http://localhost:8080/b66a23a8-6268-e611-80e2-c4346bad02e8/3.png</Url>
</ImageInfo>
</MplImages>
<SiImages />
</MplAndSiImages>
here is my c# code:
it is called on the click of an action link button and i need it to change the poition to 1 less to move it up in the list and i have the number change but the xml need s to be sorted so it has the ImageInfo nodes in the correct order.
public ActionResult MoveUp(string name, string id)
{
var pathConfig = WebConfigurationManager.AppSettings["ProductImageFolderPath"];
var url = pathConfig + id + "\\" + "ModelConfig.xml";
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlNode root = doc.DocumentElement;
XmlNode upNode = root.SelectSingleNode("/MplAndSiImages/MplImages/ImageInfo[Name/text() = '" + name + "']/pos");
string upNodeValue = upNode.InnerText;
int upNodeInt = Int32.Parse(upNodeValue);
upNodeInt = upNodeInt - 1;
var upNodeString = upNodeInt.ToString();
upNode.InnerText = upNodeString;
XmlNode downNode = root.SelectSingleNode("/MplAndSiImages/MplImages/ImageInfo/pos[text() = '" + upNodeString + "']");
string downNodeValue = downNode.InnerText;
int downNodeInt = Int32.Parse(downNodeValue);
downNodeInt = downNodeInt + 1;
var downNodeString = downNodeInt.ToString();
downNode.InnerText = downNodeString;
Func<string, int> ParseIntOrDefault = (string input) =>
{
int output;
int.TryParse(input, out output);
return output;
};
var result = doc.SelectNodes("MplAndSiImages/MplImages/*")
.Cast<XmlNode>()
.OrderBy(element => element.SelectSingleNode("pos").InnerText)
.ToList();
doc.Save(url);
return RedirectToAction("UploadAnImage", new { id = id });
}
I have seen this and tried it but is there any way of doing this with xmldocument:
XElement root = XElement.Load(xmlfile);
var orderedtabs = root.Elements("Tab")
.OrderBy(xtab => (int)xtab.Element("Order"))
.ToArray();
root.RemoveAll();
foreach(XElement tab in orderedtabs)
root.Add(tab);
root.Save(xmlfile);
I am ordering the images to display on a web page.
and when the move up button is pressed the image will be moved up in the list and swap places with the image above it.
Using linq to xml you can:
var result = XDocument.Load("data.xml")
.Descendants("ImageInfo")
.OrderBy(element => element.Element("pos")?.Value)
.ToList();
And in order to order it by the int value of it you can:
Func<string,int> ParseIntOrDefault = (string input) =>
{
int output;
int.TryParse(input, out output);
return output;
};
var result = XDocument.Load("data.xml")
.Descendants("ImageInfo")
.OrderBy(element => ParseIntOrDefault(element.Element("pos")?.Value))
.ToList();
Using XmlDocument to read the xml you can:
var doc = new XmlDocument();
doc.Load("data.xml");
var result = doc.SelectNodes("MplAndSiImages/MplImages/*")
.Cast<XmlNode>()
.OrderBy(element => element.SelectSingleNode("pos").InnerText)
.ToList();
Here too you can use the ParseIntOrDefault from above

Attempting to get single value from XDocument, nothing appears to work

I am just trying to get the value from MessageInfo.. Sender here is an excerpt of the xml. I just want the "Senders" value. I have tried many different things with XDocument, and wanted to use a Linq Query
I have tried,
var query1 = doc.Descendants("MessageInfo").Select(s => new MessageInfo
{
SYSGENID = s.Element("SysGenID").Value,
TIME_STAMP = s.Element("TimeStamp").Value,
SENDER = s.Element("Sender").Value,
RECEIVER = s.Element("Receiver").Value,
ENTITY_CODE = s.Element("EntityCode").Value
}).FirstOrDefault();
the query1 returns null. Following is example of xml
I also have tried
XDocument doc = XDocument.Load(filePath);
var messageInfo = doc.Root.Elements("MessageInfo");
var res = from m in messageInfo
select new
{
msgInfo = m.Element("MessageInfo").Value
};
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<PutSchedule xmlns="http://www.nwpp.org/eide">
<MessageInfo>
<SysGenID>4431</SysGenID>
<TimeStamp>2014-08-12T10:34:28.068000</TimeStamp>
<Sender>611</Sender>
<Receiver>WECC</Receiver>
<EntityCode>611</EntityCode>
</MessageInfo>
<Schedules>
<Schedule>
<ScheduleDescription>
<StartTime>2014-08-12T00:00:00</StartTime>
<EndTime>2014-08-15T00:00:00</EndTime>
<AccountCode>259S.NRGREEN_G1.BaseMW</AccountCode>
</ScheduleDescription>
<Quantities>
You have a namespace that you have to use.
XNamespace ns = "http://www.nwpp.org/eide";
var query1 = doc.Descendants(ns +"MessageInfo").Select(s => new MessageInfo
{
SYSGENID = s.Element(ns +"SysGenID").Value,
TIME_STAMP = s.Element(ns +"TimeStamp").Value,
SENDER = s.Element(ns +"Sender").Value,
RECEIVER = s.Element(ns +"Receiver").Value,
ENTITY_CODE = s.Element(ns +"EntityCode").Value
}).FirstOrDefault();

XML parsing with LINQ, has 2 tags with the same name but different level and not always has a value

I have the following XML:
<request>
<book>
<id>1833801</id>
<title>The Yiddish Policemen's Union </title>
<work>
<id>1234</id>
<name/>
</work>
<similar_books>
<book><id>243859</id><title>Stations of Tide</title> <isbn>0380817616</isbn>
<authors><author><id>14454</id><name>Michael Swanwick</name></author> </authors>
</book>
</similar_books>
<authors>
<author>
<id>2715</id><name>Michael Chabon</name>
<ratings_count>215884</ratings_count></author>
</authors>
<popular_shelves>
<shelf name="jewish" count="104"/><shelf name="sci-fi" count="100"/>
</popular_shelves>
</book>
</request>
I want to have all tags with their respective values, and I am using following code:
HttpWebRequest oReq = (HttpWebRequest)WebRequest.Create(uriRoot);
HttpWebResponse resp = (HttpWebResponse)oReq.GetResponse();
log.Info(" (ISBN= " + isbn10 + ") Http request has response.");
if (resp.ContentType.StartsWith("application/xml", StringComparison.InvariantCultureIgnoreCase))
{
Stream resultStreamISBN = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); //encoding for non-latin chars
StreamReader responseReader = new StreamReader(resultStreamISBN, encode);
XDocument xdoc = XDocument.Parse(responseReader.ReadToEnd());
var books = (from u in xdoc.Descendants().Elements("book")
select new
{
id = (string)u.Element("title"),
title = (string)u.Element("title"),
works = (from i in u.Elements("work")
select new
{
work_best_book_id = (int)i.Element("id"),
work_name = (string)i.Element("name"),
}).ToList(),
authors = (from i in u.Elements("authors").Elements("author")
select new
{
id = (int)i.Element("id"),
name = (string)i.Element("name"),
rating = (int)i.Element("rating_count")
}).ToList(),
popular_shelves = (from i in u.Elements("popular_shelves").Elements("shelf")
select new
{
name = (string)i.Attribute("name"),
count = (int)i.Attribute("count")
}).ToList(),
}).ToList();
The code returns null values and is not working properly. I also should note that different xml files may not have values for all the tags.
Any suggestions on how I can improve my code?
Check for missing elements like such:
name = i.Element("name") == null ? null : i.Element("name")
For value types, you make have to change them to nullable types for this to work correctly.
You can transform
id = (int)i.Element("id"),
to
id = (int?)i.Element("id"),
Whole modified code
// I am inserting the following line to demonstrate how I load the XML in test code
// Basically, I copied & pasted xml in a file OP gave named request.xml
//XDocument xdoc = XDocument.Load( #"d:\Data\request.xml");
EDIT
XDocument xdoc = XDocument.Load("http://www.goodreads.com/book/isbn?isbn=0007295685&key=lbScLXWyNGQ1q0BDoFFSg");
var books = (from u in xdoc.Descendants().Elements("book")
select new
{
id = (string)u.Element("title"),
title = (string)u.Element("title"),
works = (from i in u.Elements("work")
select new
{
work_best_book_id = (int?)i.Element("id"),
work_name = (string)i.Element("name"),
}).ToList(),
authors = (from i in u.Elements("authors").Elements("author")
select new
{
id = (int?)i.Element("id"),
name = (string)i.Element("name"),
rating = (int?)i.Element("rating_count")
}).ToList(),
popular_shelves = (from i in u.Elements("popular_shelves").Elements("shelf")
select new
{
name = (string)i.Attribute("name"),
count = (int?)i.Attribute("count")
}).ToList(),
}).ToList();

LINQ to XML Get (children?) of element?

How would I go about getting the ID information using Linq. I'm trying to add them to an array of int.
<FactionAttributes>
<name>Player</name>
<id>0</id>
<relationModifier>1</relationModifier>
<relations>
<id0>100</id0>
<id1>50</id1>
<id2>50</id2>
<id3>50</id3>
<id4>50</id4>
<id5>50</id5>
</relations>
</FactionAttributes>
That is my XML.
Here is the code I'm using so far.
void InitFactions()
{
int count = 0;
string filepath = Application.dataPath + "/Resources/factiondata.xml";
XDocument factionXML = XDocument.Load(filepath);
var factionNames = from factionName in factionXML.Root.Elements("FactionAttributes")
select new {
factionName_XML = (string)factionName.Element("name"),
factionID_XML = (int)factionName.Element("id"),
factionRelations_XML = factionName.Element("relations")// Need to turn this into array.
};
foreach ( var factionName in factionNames)
++count;
foreach ( var factionName in factionNames)
{
Factions f = new Factions();
f.otherFactionsName = new string[count];
f.otherFactionsRelation = new int[count];
int others = 0;
f.FactionName = factionName.factionName_XML;
Debug.Log(factionName.factionRelations_XML);
// Adds Rivals, not self to other list.
foreach (var factionName2 in factionNames)
{
if (factionName.factionID_XML == factionName2.factionID_XML)
continue;
f.otherFactionsName[(int)factionName2.factionID_XML] = factionName2.factionName_XML;
// THIS IS WHERE IM ADDING THE RELATIONS IN //
f.otherFactionsRelation[(int)factionName2.factionID_XML] = factionName.factionRelations_XML[(int)factionName2.factionID_XML];
Debug.Log(f.FactionName + " adds: " + factionName2.factionName_XML);
++others;
}
}
}
I have made multiple attempts using nodes and what not. I can't seem to figure out the correct syntax.
XDocument doc = XDocument.Load(Path);
//To get <id>
var MyIds = doc.Element("FactionAttributes").Element("id").Value;
//To get <id0>, <id1>, etc.
var result = doc.Element("FactionAttributes")
.Element("relations")
.Elements()
.Where(E => E.Name.ToString().Contains("id"))
.Select(E => new { IdName = E.Name, Value = E.Value});
If you want array of ints replace the select with this
.Select(E => Convert.ToInt32(E.Value)).ToArray();
If you are just after the relations Ids use this simple query
var doc = XDocument.Load("c:\\tmp\\test.xml");
var ids = doc.Descendants("relations").Elements().Select(x => x.Value);
If you want the Id and the relations ids in one array use this
var id = doc.Descendants("id").Select(x=>x.Value).Concat(doc.Descendants("relations").Elements().Select(x => x.Value));

read specific line this xml?

Is there anyway to read the specific line in this xml?
http://i.stack.imgur.com/hDPIg.jpg
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameTitle = (string)query.Element("Title"),
Gamedescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameGuide.StartsWith("See 'Payne In The Ass'")).Take(1);
this will show all guides in the xml.
This Work:
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameTitle = (string)query.Element("Title"),
Gamedescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
//GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameGuide.StartsWith("See 'Payne In The Ass'")).Take(1);
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Serious"));
So start with whatever is the first child in the XML.
Just continue your statement with a where clause: (change the condition based on your needs)
var guides = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameGuide = (string)query.Element("Guide")
};
AchivementsListBox.ItemsSource = guides.Where( ngc => ngc.GameGuide.StartsWith("Chapter 4:"));

Categories

Resources