C# Parse XML Response to Array - c#

I'm working with a third party system that returns the following xml response
{<origenxml type="data">
<data>
<item>
<id><![CDATA[PIN/4590/67]]></id>
<filename><![CDATA[CS.STAR]]></filename>
<group>
<id>MAIN</id>
<dictionary id="CS.ST.BOXNO">
<desc><![CDATA[boxes]]></desc>
<value ln="0"></value>
<raw-value ln="0"></raw-value>
<value ln="1"><![CDATA[121880 ]]></value>
<raw-value ln="1"><![CDATA[B-FILE394**BCBF*BC*121880*]]></raw-value>
<value ln="2"><![CDATA[121881 ]]></value>
<raw-value ln="2"><![CDATA[B-FILE394**BCBF*BC*121881*]]></raw-value>
<value ln="3"><![CDATA[121882 ]]></value>
<raw-value ln="3"><![CDATA[B-FILE394**BCBF*BC*121882*]]></raw-value>
<value ln="4"><![CDATA[940288 ]]></value>
<raw-value ln="4"><![CDATA[B-FILE80**BCBF*BC*940288*]]></raw-value>
<value ln="5"><![CDATA[170415 ]]></value>
<raw-value ln="5"><![CDATA[ALPHA**BC*BC*170415*]]></raw-value>
</raw-value>
</dictionary>
</group>
</item>
</data>
</origenxml>}
Each line under Boxes, represents an object where the value is the Id and the raw-value is the data ( so line 5 - ID = 170415 and the Value = ALPHA**BC*BC*170415*) but i really can't figure out the best way to parse the xml. I have no control over the response xml, so i can't anything helpful like extra node names

First thing, I want to make sure this is really the XML, because it's malformed. There's a floating end tag for the </raw-value> right before the </dictionary>. Assuming that is a mistake then the solution is easy with Linq over XML.
You'll need to add the following using statements:
using System.IO;
using System.Xml.Linq;
I created this sample in a Console application, but you should be able to easily adapt it. The first step is to create an XDocument object from your XML. I'm pulling the text from a constant string value.
static XDocument CreateDocument()
{
using (var reader = new StringReader(testData)) {
return XDocument.Load(reader);
}
}
The rest is simply to create a query and enumerate it.
var doc = CreateDocument();
var results = from value in doc.Descendants("value")
join rawValue in doc.Descendants("raw-value")
on value.Attribute("ln").Value equals rawValue.Attribute("ln").Value
select new { Value = value.Attribute("ln").Value, RawValue = rawValue.Value };
foreach (var result in results) {
Console.WriteLine($"{result.Value} => {result.RawValue}");
}

Related

Get last child element using XmlReader

Say I have this XML:
<fields>
<field fieldid="fdtElem3Group">
<value actionid="1" actiontype="review">123456789</value>
<value actionid="2" actiontype="review">123456789</value>
<value actionid="3" actiontype="review">123456789</value>
<value actionid="4" actiontype="review">123456789</value>
<value actionid="5" actiontype="review">123456789</value>
</field>
<field fieldid="fdtElem7Group">
<value actionid="1" actiontype="review">29/10/75</value>
<value actionid="2" actiontype="review">29/10/74</value>
<value actionid="3" actiontype="review">29/10/74</value>
<value actionid="4" actiontype="review">29/10/76</value>
<value actionid="5" actiontype="review">29/10/74</value>
</field>
</fields>
I'm trying to get the value of the last 'value' element of each respective 'field' element using XmlReader. How would I do that please? This doesn't work:
while (xmlReader.Read())
{
if ((xmlReader.NodeType == System.Xml.XmlNodeType.Element) && (xmlReader.Name == "field"))
{
xmlReader.ReadToDescendant("value");
while (xmlReader.ReadToNextSibling("value"))
{
//just iterate over until it reaches the end
}
xmlReader.Read();
Console.WriteLine(xmlReader.Value);
}
}
Sorry, just read now you're looking for an xmReader solution. But using XDocument and Linq, you could do the following:
string xml = #"<fields>
<field fieldid=""fdtElem3Group"">
<value actionid=""1"" actiontype=""review"">123456789</value>
<value actionid=""2"" actiontype=""review"">123456789</value>
<value actionid=""3"" actiontype=""review"">123456789</value>
<value actionid=""4"" actiontype=""review"">123456789</value>
<value actionid=""5"" actiontype=""review"">123456789</value>
</field>
<field fieldid=""fdtElem7Group"">
<value actionid=""1"" actiontype=""review"">29/10/75</value>
<value actionid=""2"" actiontype=""review"">29/10/74</value>
<value actionid=""3"" actiontype=""review"">29/10/74</value>
<value actionid=""4"" actiontype=""review"">29/10/76</value>
<value actionid=""5"" actiontype=""review"">29/10/74</value>
</field>
</fields>";
var xmlDoc = XDocument.Parse(xml).Root;
var lastElements = xmlDoc.Descendants("field").Select(x => x.LastNode);
Hope this helps !
static void Main(string[] args)
{
string xml = #"<fields><field fieldid='fdtElem3Group'><value actionid='1' actiontype='review'>123456789</value><value actionid='2' actiontype='review'>123456789</value><value actionid='3' actiontype='review'>123456789</value><value actionid='4' actiontype='review'>123456789</value><value actionid='5' actiontype='review'>123456789</value></field><field fieldid='fdtElem7Group'><value actionid='1' actiontype='review'>29/10/75</value> <value actionid='2' actiontype='review'>29/10/74</value><value actionid='3' actiontype='review'>29/10/74</value><value actionid='4' actiontype='review'>29/10/76</value><value actionid='5' actiontype='review'>29/10/74</value></field></fields>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
foreach (XmlNode item in xmlDocument.DocumentElement.ChildNodes)
{
Console.WriteLine(item.LastChild.InnerXml);
}
Console.ReadKey();
}
If you don't want to load your entire XML into an XDocument (because, e.g., it is very large), you can adopt a hybrid approach where you use an XmlReader to iterate through the <field> and <value> elements in your XML file, load each <value> element into an XElement, then select the last one of each parent <field>. This keeps the memory footprint small and constant no matter how large the XML file grows.
First introduce the following two extension methods:
public static class XmlReaderExtensions
{
public static IEnumerable<IEnumerable<XElement>> ReadNestedElements(this XmlReader xmlReader, string outerName, string innerName)
{
while (!xmlReader.EOF)
{
if (xmlReader.NodeType == System.Xml.XmlNodeType.Element && xmlReader.Name == outerName)
{
using (var subReader = xmlReader.ReadSubtree())
{
yield return subReader.ReadElements(innerName);
}
}
xmlReader.Read();
}
}
public static IEnumerable<XElement> ReadElements(this XmlReader xmlReader, string name)
{
while (!xmlReader.EOF)
{
if (xmlReader.NodeType == System.Xml.XmlNodeType.Element && xmlReader.Name == name)
{
var element = (XElement)XNode.ReadFrom(xmlReader);
yield return element;
}
else
{
xmlReader.Read();
}
}
}
}
Then your algorithm to get the last 'value' element of each respective 'field' element becomes very simple:
public static List<string> LastFieldValues(XmlReader reader)
{
var query = reader.ReadNestedElements("field", "value")
.Select(l => l.LastOrDefault())
.Where(v => v != null)
.Select(v => (string)v);
return query.ToList();
}
Notes:
XmlReaderExtensions.ReadNestedElements() returns an enumerable of enumerables of <value> elements belonging to <field> elements. Enumerable.LastOrDefault() is then used to select the last <value> belonging to each <field>.
Whenever working directly with XmlReader, be sure to test XML both with and without indentation, for reasons explained here and here.
XmlReader.ReadSubtree() leaves the XmlReader positioned on the EndElement node of the element being read, while XNode.ReadFrom() leaves the reader positioned immediately after the EndElement node of the element being read. Just an annoying inconsistency to watch out for.
On the other hand, if you are willing to load the entire XML into memory as an XDocument, this can be done very simply using XPathSelectElements():
// Parse the XML into an XDocument.
// You can use use XDocument.Load(fileName) to load from a file
var doc = XDocument.Parse(xmlString);
var xpathLastValues = doc
.XPathSelectElements(#"//fields/field/value[last()]")
.Select(e => e.Value)
.ToList();
Sample fiddle.

Grouping in LINQ to XML with "any" or first" aggregation

I need to group following XML using LINQ to XML or XPath by param/Type. With grouping I would like to get any (or first, doesn't matter) value of id tag.
<list>
<item>
<id>1</id>
<param>
<type>A</type>
</param>
</item>
<item>
<id>2</id>
<param>
<type>A</type>
</param>
</item>
<item>
<id>3</id>
<param>
<type>B</type>
</param>
</item>
<item>
<id>4</id>
<param>
<type>B</type>
</param>
</item>
Desirable results is
A - 1
B - 3
I've tried
var content = from item in doc.Descendants("item").Descendants("param")
group item by new
{
mType = (String)item.Element("type"),
} into g
select new
{
mType = g.Key.mType,
};
but I cannot figure out how to reference ID that is higher in hierarchy , or how to reference PARAM/TYPE when selecting ID.
I would suggest using System.Xml.Linq (XDocument)
If my understanding is good, here is what I've done:
var xml = "<list><item><id>1</id><param><type>A</type></param></item><item><id>2</id><param><type>A</type></param></item><item><id>3</id><param><type>B</type></param></item><item><id>4</id><param><type>B</type></param></item></list>";
var document = XDocument.Parse(xml);
foreach (var param in document.Root.Elements("item").GroupBy(i => i.Element("param").Element("type").Value))
{
var firstId = param.First().Element("id").Value;
Console.WriteLine ("The first of {0} = {1}", param.Key, firstId);
}
the output is :
The first of A = 1
The first of B = 3
In addition to what was suggested by Cedric, you could accomplish the same thing in pure XPath:
var xml = "<list><item><id>1</id><param><type>A</type></param></item><item><id>2</id><param><type>A</type></param></item><item><id>3</id><param><type>B</type></param></item><item><id>4</id><param><type>B</type></param></item></list>";
var document = XDocument.Parse(xml);
// start at the root, then grab the first item element which has a param/type element whose value is equal to 'B'
var answer = document.Root.XPathSelectElement("./item[./param/type='B'][1]").Element("id").Value;
In XPath, the square brackets function effectively like a where clause. My first set of square brackets qualify that I want an item that contains a matching param/type element. The second set of square brackets limit this down to the first match.

Parse XML into String type array in Windows Phone 8 App using C#

I have an XML file, which I like to parse and get the value in a string type array. I know there are XMLSerialization namespace and other things. But what I am trying to achieve is getting the value in a string array. It may be obtained using Foreach loop or for loop.
For example, here is my XML file:
<?xml version="1.0" encoding="utf-8" ?>
<channel>
<title>Social Media</title>
<item>
<title>Facebook</title>
<link>http://www.facebook.com/</link>
</item>
<item>
<title>Twitter</title>
<link>http://www.twitter.com/</link>
</item>
<item>
<title>Google+</title>
<link>http://plus.google.com/</link>
</item>
</channel>
Now, I have two string type array as variable into a C# file.
For example:
public string[] WebsiteName;
public string[] Urls;
Now, I want to get all the values of WebsiteName into the WebsiteName array and website links into the Urls array.
Is there any way to do it? If yes, please show it to me. It will be very helpful.
Here is an example to get website names and links using LINQ:
var xml = #"<?xml version=""1.0"" encoding=""utf-8"" ?>
<channel>
<title>Social Media</title>
<item>
<title>Facebook</title>
<link>http://www.facebook.com/</link>
</item>
<item>
<title>Twitter</title>
<link>http://www.twitter.com/</link>
</item>
<item>
<title>Google+</title>
<link>http://plus.google.com/</link>
</item>
</channel>";
var doc = XDocument.Parse(xml);
WebsiteName = doc.Descendants("title").Select(o => o.Value).ToArray();
Urls = doc.Descendants("link").Select(o => o.Value).ToArray();
XDocument.Parse(xml): create XDocument from string. If you want the source is file instead of string then you can use XDocument.Load("path_to_the_xml_file").
doc.Descendants("title"): will get all tags named "title", then
.Select(o => o.Value): will get the string between the opening and closing tag, aka the value
var doc = XDocument.Parse(xml);
var WebsiteName = doc.Descendants("title").Select(o => o.Value).ToArray();
ListBox.ItemsSource = WebsiteName;
And you get content written in listbox.

Trouble Parsing XML

I am having some trouble parsing some XML from centovacast v3 XML API. I've worked with their 2.x API and parsed it, but the responses have totally changed and I cannot seem to make any of my existing parsers work. Every example I've tried I cannot seem to get at the data correctly.
I'm using .NET 3.5 (4.0 is acceptable as well), any examples would be greatly appreciated.
An example XML document is:
<?xml version=""1.0"" encoding=""UTF-8""?>
<centovacast version=""3.0.0"" host=""0.0.0.0:2199"">
<response type=""success"">
<message>OK</message>
<data>
<row>
<id>1</id>
<parameters>
<ipaddress>127.0.0.1</ipaddress>
<port>2198</port>
<title>Local server</title>
<isrelay>1</isrelay>
<ismaster>1</ismaster>
<defaultip>0.0.0.0</defaultip>
<daemontype>RPC</daemontype>
<hostname/>
</parameters>
<status>
<memfree>101879808</memfree>
<memtotal>1073741824</memtotal>
<memavail>778653696</memavail>
<swapfree>1077501952</swapfree>
<swaptotal>1077501952</swaptotal>
<buffers>172535808</buffers>
<cpuload>0.00</cpuload>
<uptime>13372713</uptime>
<machine>Intel(R) Xeon(R) CPU E5620</machine>
<osbrief>Linux</osbrief>
<osdetails>2.6.18</osdetails>
<other>
<Processes>
<field>n</field>
<field>72</field>
</Processes>
<Kernel>
<field>s</field>
<field>Linux version 2.6.18</field>
</Kernel>
<row>
<field>f</field>
<field>0.000000</field>
</row>
<row>
<field>f</field>
<field>0.000000</field>
</row>
<row>
<field>f</field>
<field>0.000000</field>
</row>
</other>
<online>1</online>
</status>
<accounts>
<licensed>-1</licensed>
<active>1</active>
<inactive>0</inactive>
</accounts>
</row>
</data>
</response>
</centovacast>
I've tried using the following code:
var xml = XDocument.Parse(xmldata);
var query = from p in xml.Descendants("status")
select p;
foreach (var record in query)
MessageBox.Show(record.Value);
but it returns all the data inside the <status> and <parameters> in one big jumble, rather then in separate values.
I would love to serialize / deserialize, as the XML call I'm making returns the above for each server in the cluster, so it could be quite a large result set, but I am not picky, I would be happy just being able to get the data into the correct variables so I can use them.
Here's an example storing some of the elements in an anonymous type:
var data =
XDocument.Parse(xml)
.Root
.Element("response")
.Element("data")
.Elements("row")
.Select(row =>
new
{
Id = Int32.Parse(row.Element("id").Value),
Parameters = new
{
IpAddress = row.Element("parameters").Element("ipaddress").Value,
port = Int32.Parse(row.Element("parameters").Element("port").Value),
},
Status = new
{
MemFree = Int32.Parse(row.Element("status").Element("memfree").Value),
},
});
You can always plug in your own concrete types and null checks where option values might be.
xml.Descendants("status") returns the whole element status together with it's child elements. If you want to enumerate it's elements use the following code:
xml.Descendants("status").Descendants();

Entity Framework returns incomplete xml data

EntityFramework returns incomplete data for xml field.
I Serialize data and save this in an xml field in the db. The missing data is in the Images node value in the xml is also serialized a serilized object. The ecoding of the value happens when i serialze the field object.
Where is my missing data from the Images -> Value field and why does it dissapear?
This is what i have in my ms sql xml field:
<ArrayOfField xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Field>
<Key>Images</Key>
<Value><ArrayOfImage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Image>
<Name>Penguins.jpg</Name>
<Path>~/Fileshare/Pages/6/Penguins.jpg</Path>
<AltText>Test</AltText>
</Image>
<Image>
<Name>Tulips.jpg</Name>
<Path>~/Fileshare/Pages/6/Tulips.jpg</Path>
<AltText>Test</AltText>
</Image>
</ArrayOfImage></Value>
</Field>
<Field>
<Key>Test</Key>
<Value>Test</Value>
</Field>
<Field>
<Key>MyEditor</Key>
<Value />
</Field>
</ArrayOfField>
This is what EF returns:
<ArrayOfField xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Field><Key>Images</Key><Value><ArrayOfImage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /></Value></Field><Field><Key>Test</Key><Value>Test</Value></Field><Field><Key>MyEditor</Key><Value /></Field></ArrayOfField>
I get data from EntityFramework like this:
public Item Get(int id)
{
using (var context = new Entities())
{
var item = context.Items.SingleOrDefault(x => x.ID == id);
return item;
}
}
I think you need to write a stored procedure to return the xml field data. For Entity Framework, you need to write something like following to get the full list of xml data
EntityCommand command = connection.CreateCommand();
command.CommandText = "XXXX";
command.CommandType = CommandType.StoredProcedure;
using (EntityDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (reader.Read())
{
str = str + reader.GetString(0);
}
}
I think either the data is lost during deserialization, or it's a mistake due to fatigue on your part (i mean, you might be looking at a different entity when comparing and you think there is something wrong).
Either way, an alternative to rule out the serialization issue is to save it serialized as something else or avoid having an xml in an xml (even though there shouldn't be any problem) by just adding the ArrayOfImages as a class member and not just another key/value pair in your array.

Categories