Parsing xml. return one object not collection - c#

I have a xml file:
<Result>Ok</Result>
<Error></Error>
<Remark></Remark>
<Data>
<Movies>
<Movie ID='2'>
<Name><![CDATA[TestName]]></Name>
<Duration Duration='170'>2h 50min</Duration>
<Properties>
<Property Name='11'><![CDATA[1111110]]></Property>
</Properties>
<Rental from_date='' to_date=''>
<SessionCount></SessionCount>
<PU_NUMBER></PU_NUMBER>
</Rental>
</Movie>
</Movies>
</Data>
</XML>
Code for pasring xml file:
var results = from element in XDocument.Parse(queryResponse).Descendants("Movie")
select new BaseEvent
{
OID = (int)element.Attribute("ID"),
Subject = (string)element.Element("Name"),
Duration = (int)element.Element("Duration").Attribute("Duration")
};
The problem in that Descedants retruns IEumerable<BaseEvent> but I want that will be BaseEvent. How can I do this?

Just use First(), Last(), Single(), FirstOrDefault() etc.
Personally I'd do that initially, rather than doing it all in a query:
var element = XDocument.Parse(queryResponse)
.Descendants("Movie")
.FirstOrDefault();
if (element == null)
{
// Handle the case of no movies
}
else
{
var baseEvent = new BaseEvent
{
OID = (int) element.Attribute("ID"),
Subject = (string) element.Element("Name"),
Duration = (int) element.Element("Duration")
.Attribute("Duration")
};
// Use baseEvent
}

Add a .First() to get the first element:
from element in XDocument.Parse(queryResponse).Descendants("Movie")
select new BaseEvent
{
OID = (int)element.Attribute("ID"),
Subject = (string)element.Element("Name"),
Duration = (int)element.Element("Duration").Attribute("Duration")
}.First();
You can alternatively use FirstOrDefault() (in case there are no such nodes), Last() or Single().

Related

Xml file with C# can't get the value

Here is my xml file
<?xml version="1.0" encoding="ASCII"?>
<Vitals>
<Vendor>General Electric Healthcare</Vendor>
<Model>Pro/Procare</Model>
<Result name="Mean_arterial_pressure">
<Value>86</Value>
<Units name="mmHg"></Units>
<Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
</Result>
<Result name="Systolic_blood_pressure">
<Value>130</Value>
<Units name="mmHg"></Units>
<Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
</Result>
<Result name="Diastolic_blood_pressure">
<Value>67</Value>
<Units name="mmHg"></Units>
<Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
</Result>
<Result name="Pulse">
<Value>73</Value>
<Units name="BPM"></Units>
<Method>blood_pressure</Method>
<Time_stamp year="2008" month="11" day="18" hour="12" minute="33" second="14"></Time_stamp>
</Result>
</Vitals>
and Here is my sourcecode, I having the issue how to get the result name and the value?
private void btnReadXml_Click(object sender, EventArgs e)
{
Hashtable h = new Hashtable();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\dinamap.xml");
XmlNodeList doc_results = xmlDoc.GetElementsByTagName("Vitals/Vendor/Model/Result[#name='Systolic_blood_pressure']");
foreach (XmlNode pnode in doc_results)
{
foreach (XmlNode cnode in pnode.ChildNodes)
{
if (cnode.Name == "Value")
{
h.Add(pnode.Attributes["name"].InnerText + cnode.Name, cnode.Attributes["name"].InnerText);
}
else
{
h.Add(pnode.Attributes["name"].InnerText + "_" + cnode.Name, cnode.InnerText);
}
}
}
}
May I know what wrong of my code? always can't get the value.
I need to get the value from xml file.
On line 5, your xPath specify Model is a child of Vendor while Vendor contains only a string (<Vendor>General Electric Healthcare</Vendor>).
Moreover, to navigate with xPath i advice you to use SelectNodes function.
Try this instead :
XmlNodeList doc_results = xmlDoc.SelectNodes("/Vitals/Model/Result[#name='Systolic_blood_pressure']");
It'd be easier if you used LINQ to XML for this.
var doc = XDocument.Load("path/to/xml");
var results =
from result in doc.Descendants("Result")
select new
{
Name = (string) result.Attribute("name"),
Value = (string) result.Element("Value"),
Units = (string) result.Element("Units").Attribute("name")
};
You could then filter by Name if you needed:
var sysBp = results.Single(x => x.Name == "Systolic_blood_pressure");
See this fiddle for a working demo.
You're currently trying to retrieve the "name" attribute of the child node, which it does not actually have in the case of your value node. If you use cnode.Value you can get the contents of a text note.
Also, Result is not a child of Model, which is not a child of Vendor. You'll either want to properly make them children or change the path you're setting for doc_results
private void btnReadXml_Click(object sender, EventArgs e)
{
var doc = XDocument.Load("C:\\dinamap.xml");
var results = from result in doc.Descendants("Result")
select new
{
Name = (string)result.Attribute("name"),
Value = (string)result.Element("Value"),
Units = (string)result.Element("Units").Attribute("name")
};
foreach (var result in results)
{
MessageBox.Show("{result.Name}: {result.Value} {result.Units}");
}
}

C# deserialize xml using linq

I have the following xml file
<?xml version="1.0" encoding="utf-8"?>
<Launchpad>
<Shortcuts>
<Shortcut Id="1">
<Type>Folder</Type>
<FullPath>C:\bla\bla\bla</FullPath>
<Name>Proximity</Name>
</Shortcut>
<Shortcut Id="2">
<Type>Folder</Type>
<FullPath>C:\bla</FullPath>
<Name>Visual Studio 2017</Name>
</Shortcut>
</Shortcuts>
</Launchpad>
I am trying to deserialize to an object like this: (Tried query syntax first, didn't work either)
XDocument xd = XDocument.Load(FullPath);
// query syntax
//var shortcuts = (from s in xd.Descendants("Shortcuts")
// select new Shortcut()
// {
// Id = Convert.ToInt32(s.Attribute("Id")),
// TypeOfLink = GetTypeFromString(s.Descendants("Type")
// .First()
// .Value),
// FullPathToTarget = s.Descendants("FullPath")
// .First()
// .Value,
// Name = s.Descendants("Name").First().Value,
// }).ToList();
// method syntax
List<Shortcut> shortcuts = xd.Descendants("Shortcuts")
.Select(s =>
new Shortcut()
{
//Id = Convert.ToInt32(s.Attribute("Id")),
TypeOfLink = GetTypeFromString(s.Descendants("Type")
.First().Value),
FullPathToTarget = s.Descendants("FullPath")
.First().Value,
Name = s.Descendants("Name")
.First().Value,
}).ToList();
return shortcuts;
For some reason I only get a single shortcut object in the list. Also, for some reason, the s.Attribute("Id") is null.
If anyone has any suggestions to improve the linq query or why the attribute is not working it would be a great help
You have to read .Descendants("Shortcut") instead of .Descendants("Shortcuts").
Something like this:
List<Shortcut> shortcuts = xd.Descendants("Shortcut").Select(s =>
new Shortcut()
{
Id = s.Attribute("Id").Value,
TypeOfLink = s.Descendants("Type").First().Value,
FullPathToTarget = s.Descendants("FullPath").First().Value,
Name = s.Descendants("Name").First().Value,
}).ToList();
I get the full list by selecting the Shortcut as a descendant of Shortcuts.
Also, ID is an XAttribute so you cant convert it to int.
You need to use Value to get the attributes value.
XDocument xd = XDocument.Load(ms);
XElement root = xd.Document.Root;
var list = root.Descendants("Shortcuts").Descendants("Shortcut").Select(x =>
new Shortcut()
{
Id = Convert.ToInt32(x.Attribute("Id").Value),
TypeOfLink = GetTypeFromString(x.Descendants("Type")
.First().Value),
FullPathToTarget = x.Descendants("FullPath")
.First().Value,
Name = x.Descendants("Name").First().Value
}).ToList();

XElement.Elements() returning empty collection

I'm trying to get the values from an XElement that I'm receiving from db notification.
The xml structure is like this:
<?xml version="1.0"?>
<root>
<inserted>
<row>
<CODI_AVERIA>22</CODI_AVERIA>
<NUMERO_LINIA>2</NUMERO_LINIA>
<DIA>2016-07-17T00:00:00</DIA>
<HORA>1899-12-30T10:26:15.790</HORA>
<CODI_USUARI>1</CODI_USUARI>
<ACCIO>0</ACCIO>
<CODI_PSEUDO>-1</CODI_PSEUDO>
</row>
</inserted>
</root>
And this is the method that I'm using to get the data, and it returns me a List that is empty.
static void getAccio(XElement xml)
{
try
{
xml.Descendants("deleted").Remove();
var items = xml.Elements("row").Select(n => new
{
Codi_averia = n.Element("CODI_AVERIA").Value,
Numero_linia = n.Element("NUMERO_LINIA)").Value,
Accio = n.Element("ACCIO").Value
}).ToList();
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
I have tried to get the value of each field apart and it doesn't allow me to get them separetly as XElements.
Use .Descendants() instead of .Elements():
var items = xml.Descendants("row").Select(n => new
{
Codi_averia = n.Element("CODI_AVERIA").Value,
Numero_linia = n.Element("NUMERO_LINIA)").Value,
Accio = n.Element("ACCIO").Value
}).ToList();
.Elements finds only those elements that are direct descendents, i.e. immediate children. - And I assume that your XElement is the parent of the inserted section.
For difference between .Element and .Descendants see this question
If you don't want to find them in all inner elements too then do .Element("inserted").Elements("rows")
Your document doesn't have any root element with the name row. Instead, you need to select inserted first, and enumerate the row elements of inserted:
var xml = #"<inserted>
<row>
<CODI_AVERIA>21</CODI_AVERIA>
<NUMERO_LINIA>2</NUMERO_LINIA>
<DIA>2016-07-17T00:00:00</DIA>
<HORA>1899-12-30T10:26:15.790</HORA>
<CODI_USUARI>1</CODI_USUARI>
<ACCIO>0</ACCIO>
<CODI_PSEUDO>-1</CODI_PSEUDO>
</row>
</inserted>";
var xdoc = XDocument.Parse(xml);
var items = xdoc.Element("inserted").Elements("row").Select(n => new
{
Codi_averia = n.Element("CODI_AVERIA").Value,
Numero_linia = n.Element("NUMERO_LINIA").Value,
Accio = n.Element("ACCIO").Value
}).ToList();
In your sample code ("NUMERO_LINIA)") is wrong because of additionnal quote and parenthesis.
This works for me :
XDocument xd = XDocument.Load("XMLFile1.xml");
var lst = (from n in xd.Descendants("row")
select new
{
Codi_averia = n.Element("CODI_AVERIA").Value,
Numero_linia = n.Element("NUMERO_LINIA").Value,
Accio = n.Element("ACCIO").Value
}).ToList();
foreach (var item in lst)
Console.WriteLine(string.Format("{0}{1}{2}", item.Codi_averia, item.Numero_linia, item.Accio));

Reading XML and creating a frequency count of elements in C#

I have an XML file in this format (but only much bigger) :
<customer>
<name>John</name>
<age>24</age>
<gender>M</gender>
</customer>
<customer>
<name>Keith</name>
<age></age> <!--blank value-->
<gender>M</gender>
</customer>
<customer>
<name>Jenny</name>
<age>21</age>
<gender>F</gender>
</customer>
<customer>
<name>John</name>
<age>24</age> <!--blank value-->
<gender>M</gender> <!--blank value-->
</customer>
I want to generate a DataTable which will be in this format :
Element Name   Value   Frequency name       filled     4name       blank    0age        filled     2age       blank    2gender      filled    3gender      blank    1
Currently I am completing this task in 2 parts, first creating a DataTable structure as above and setting all the frequencies to 0 as default. And then reading the XML using XmlReader, and increasing the count of the frequency everytime XmlReader finds a child element.
My problem is that the second function that I use for adding the actual count is taking too long for very big Xml files with many customers having many attributes. How can I improve the efficiency of this function? My code :
static void AddCount(DataTable dt)
{
int count;
using (XmlReader reader = XmlReader.Create(#"C:\Usr\sample.xml"))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
string eleName = reader.Name;
DataRow[] foundElements = dt.Select("ElementName = '" + eleName + "'");
if (!reader.IsEmptyElement)
{
count = int.Parse(foundElements.ElementAt(0)["Frequency"].ToString());
foundElements.ElementAt(0).SetField("Frequency", count + 1);
}
else
{
count = int.Parse(foundElements.ElementAt(0)["Frequency"].ToString());
foundElements.ElementAt(0).SetField("Frequency", count + 1);
}
}
}
}
}
I am also ready to change the XmlReader class for any other more efficient class for this task. Any advice is welcome.
It turned out that querying in the DataTable using the Select operation was very expensive and that was making my function very slow.
Instead of that, used a Dictionary<string, ValueFrequencyModel> and queried on that to fill the dictionary with the count, and after completing that, converted the Dictionary<string, ValueFrequencyModel> into a DataTable.
This saved loads of time for me and solved the problem.
You can use following code:
using (XDocument xdoc = XDocument.Load(#"C:\Users\aks\Desktop\sample.xml"))
{
var customers = xdoc.Descendants("customer");
var totalNodes = customers.Count();
var filledNames = customers.Descendants("name").Where(x => x.Value != string.Empty).Count();
var filledAges = customers.Descendants("age").Where(x => x.Value != string.Empty).Count();
var filledGenders = customers.Descendants("gender").Where(x => x.Value != string.Empty).Count();
var unfilledNames = totalNodes - filledNames;
var unfilledAges = totalNodes - filledAges;
var unfilledGenders = totalNodes - filledGenders;
}
Try this logic, currently I have only taken only one attribute here ie Name
XDocument xl = XDocument.Load(#"C:\Usr\sample.xml");
var customers = xl.Descendants("Customer");
var customerCount = customers.Count();
var filledCustomers = customers.Where(x => x.Element("Name").Value != string.Empty).Count();
var nonfilledCustomers = customerCount - filledCustomers;

Reading XML file into memory and searching it

I have a given XML file that I need to process. For the sake of argument, let's say I've already loaded it in as a string.
<?xml version="1.0" encoding="UTF-8" ?>
<GROUP ID="_group_id" ORDERINFO="00000" TITLE="Group 1">
<GROUP ID="_group_id_2" TITLE="Group 2">
<LO ID="_id_code1" LANG="enUS" TYPE="_cust" TITLE="Title 1" />
<LO ID="_id_code2" LANG="enUS" TYPE="_cust" TITLE="Title 2" />
</GROUP>
<GROUP ID="_group_id_3" TITLE="Group 3">
<LO ID="_id_code1" LANG="enUS" TYPE="_cust" TITLE="Title 1" />
<LO ID="_id_code2" LANG="enUS" TYPE="_cust" TITLE="Title 2" />
</GROUP>
</GROUP>
There can be many LOs and many GROUPs in a given XML file. I've been trying various methods with no luck. I need something that will find the matching LO by ID to a given string and then allow me to retrieve the corresponding TYPE and TITLE into strings so that I may use them for processing.
I tried reading the file into an XmlDocument but once loaded I could not figure out how to find the appropriate elements.
Sorry for post prior to edit - some text got cut off
You can use XmlDocument or XDocument to parse the Xml.
Here is an example with XDocument:
Data class:
public class Lo
{
public string Id { get; set; }
public string Lang { get; set; }
public string Type { get; set; }
public string Title { get; set; }
}
Code:
var document = XDocument.Parse(data);
var value = "_id_code1";
IEnumerable<Lo> result =
document.XPathSelectElements(".//LO")
.Where(x => x.Attribute("ID").Value == value)
.Select(x =>
new Lo
{
Id = x.Attribute("ID").Value,
Lang = x.Attribute("LANG").Value,
Type = x.Attribute("TYPE").Value,
Title = x.Attribute("TITLE").Value
});
When loaded into a XmlDocument, you can use XPath to locate notes.
E.g:
XmlNode group = xmlDocument.SelectSingleNode("/GROUP/GROUP[#ID='_group_id_2']");
Or:
XmlNodeList groups = xmlDocument.SelectNodes("/GROUP/GROUP");
foreach(XmlNode group in groups)
{
string id = group.Attributes["ID"].Value;
}
It is very easy. For a more complete walk through you should search the internet.
See the documentation:
Overview of XML in the .NET Framework.
XML Processing Options in the .NET Framework
It's better to cast XAtribute to string, then access its Value property (if some attribute not found, you will get null instead of exception). Also query syntax is more compact here
string id = "_id_code1";
XDocument xdoc = XDocument.Parse(xml);
var query = from lo in xdoc.Descendants("LO")
where (string)lo.Attribute("ID") == id
select new {
Id = (string)lo.Attribute("ID"),
Language = (string)lo.Attribute("LANG"),
Type = (string)lo.Attribute("TYPE"),
Title = (string)lo.Attribute("TITLE")
};
This query will return sequence of anonymous objects with properties Id, Language, Type, Title. You can enumerate them with foreach.
I did small test application for this, I included your xml as the string.
var xmlMessage = #"keep your xml here, I removed due to formatting";
var matchedElements = XDocument.Parse(xmlMessage).Descendants().Where(el => el.Name == "LO" && el.Attribute("ID").Value == "_id_code1");
foreach (var el in matchedElements)
{
Console.WriteLine("ElementName : {0}\nID = {1}\nLANG = {2}\nTYPE = {3}\nTITLE = {4}\n"
, el.Name.LocalName, el.Attribute("ID").Value, el.Attribute("LANG").Value, el.Attribute("TYPE").Value, el.Attribute("TITLE").Value);
}
This would help you to fetch all LO elements having the ID "_id_code" irrespective of the GROUP element.
If you need to consider the group, replace the second line code with this:
var matchedElements = XDocument.Parse(xmlMessage).Descendants().Where(el => el.Parent != null && el.Parent.Attribute("ID").Value == "_group_id_2" && el.Name == "LO" && el.Attribute("ID").Value == "_id_code1");
Here, I'm checking for the "_group_id_2", you can replace with your group id.
The required namespaces:
using System.Linq;
using System.Xml;
using System.Xml.Linq;

Categories