Find frequency of values in an Array or XML (C#) - c#

I have an XML feed (which I don't control) and I am trying to figure out how to detect the volume of certain attribute values within the document.
I am also parsing the XML and separating attributes into Arrays (for other functionality)
Here is a sample of my XML
<items>
<item att1="ABC123" att2="uID" />
<item att1="ABC345" att2="uID" />
<item att1="ABC123" att2="uID" />
<item att1="ABC678" att2="uID" />
<item att1="ABC123" att2="uID" />
<item att1="XYZ123" att2="uID" />
<item att1="XYZ345" att2="uID" />
<item att1="XYZ678" att2="uID" />
</items>
I want to find the volume nodes based on each att1 value. Att1 value will change. Once I know the frequency of att1 values I need to pull the att2 value of that node.
I need to find the TOP 4 items and pull the values of their attributes.
All of this needs to be done in C# code behind.
If I was using Javascript I would create an associative array and have att1 be the key and the frequency be the value. But since I'm new to c# I don't know how to duplicate this in c#.
So I believe, first I need to find all unique att1 values in the XML. I can do this using:
IEnumerable<string> uItems = uItemsArray.Distinct();
// Where uItemsArray is a collection of all the att1 values in an array
Then I get stuck on how I compare each unique att1 value to the whole document to get the volume stored in a variable or array or whatever data set.
Here is the snippet I ended up using:
XDocument doc = XDocument.Load(#"temp/salesData.xml");
var topItems = from item in doc.Descendants("item")
select new
{
name = (string)item.Attribute("name"),
sku = (string)item.Attribute("sku"),
iCat = (string)item.Attribute("iCat"),
sTime = (string)item.Attribute("sTime"),
price = (string)item.Attribute("price"),
desc = (string)item.Attribute("desc")
} into node
group node by node.sku into grp
select new {
sku = grp.Key,
name = grp.ElementAt(0).name,
iCat = grp.ElementAt(0).iCat,
sTime = grp.ElementAt(0).sTime,
price = grp.ElementAt(0).price,
desc = grp.ElementAt(0).desc,
Count = grp.Count()
};
_topSellers = new SalesDataObject[4];
int topSellerIndex = 0;
foreach (var item in topItems.OrderByDescending(x => x.Count).Take(4))
{
SalesDataObject topSeller = new SalesDataObject();
topSeller.iCat = item.iCat;
topSeller.iName = item.name;
topSeller.iSku = item.sku;
topSeller.sTime = Convert.ToDateTime(item.sTime);
topSeller.iDesc = item.desc;
topSeller.iPrice = item.price;
_topSellers.SetValue(topSeller, topSellerIndex);
topSellerIndex++;
}
Thanks for all your help!

Are you using .NET 3.5? (It looks like it based on your code.) If so, I suspect this is pretty easy with LINQ to XML and LINQ to Objects. However, I'm afraid it's not clear from your example what you want. Do all the values with the same att1 also have the same att2? If so, it's something like:
var results = (from element in items.Elements("item")
group element by element.Attribute("att1").Value into grouped
order by grouped.Count() descending
select grouped.First().Attribute("att2").Value).Take(4);
I haven't tested it, but I think it should work...
We start off with all the item elements
We group them (still as elements) by their att1 value
We sort the groups by their size, descending so the biggest one is first
From each group we take the first element to find its att2 value
We take the top four of these results

If you have the values, you should be able to use LINQ's GroupBy...
XDocument doc = XDocument.Parse(xml);
var query = from item in doc.Descendants("item")
select new
{
att1 = (string)item.Attribute("att1"),
att2 = (string)item.Attribute("att2") // if needed
} into node
group node by node.att1 into grp
select new { att1 = grp.Key, Count = grp.Count() };
foreach (var item in query.OrderByDescending(x=>x.Count).Take(4))
{
Console.WriteLine("{0} = {1}", item.att1, item.Count);
}

You can use LINQ/XLINQ to accomplish this. Below is a sample console application I just wrote, so the code might not be optimized but it works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
namespace FrequencyThingy
{
class Program
{
static void Main(string[] args)
{
string data = #"<items>
<item att1=""ABC123"" att2=""uID"" />
<item att1=""ABC345"" att2=""uID"" />
<item att1=""ABC123"" att2=""uID"" />
<item att1=""ABC678"" att2=""uID"" />
<item att1=""ABC123"" att2=""uID"" />
<item att1=""XYZ123"" att2=""uID"" />
<item att1=""XYZ345"" att2=""uID"" />
<item att1=""XYZ678"" att2=""uID"" />
</items>";
XDocument doc = XDocument.Parse(data);
var grouping = doc.Root.Elements().GroupBy(item => item.Attribute("att1").Value);
foreach (var group in grouping)
{
var groupArray = group.ToArray();
Console.WriteLine("Group {0} has {1} element(s).", groupArray[0].Attribute("att1").Value, groupArray.Length);
}
Console.ReadKey();
}
}
}

Related

List is empty after parsing XML with LinQ

I have an xml file similar to the following:
<doc>
<file>
<header>
<source>
RNG
</source>
</header>
<body>
<item name="items.names.id1">
<property>propertyvalue1</property>
</item>
<!-- etc -->
<item name="items.names.id100">
<property>propertyvalue100</property>
</item>
<!-- etc -->
<item name="otheritems.names.id100">
<property>propertyvalue100</property>
</item>
</body>
</file>
</doc>
And the following class:
private class Item
{
public string Id;
public string Property;
}
The file has, for example, 100 item entries (labeled 1 to 100 in the name attribute). How can I use Linq Xml to get hold of these nodes and place them a in list of item?
Using Selman22's example, I'm doing the following:
var myList = xDoc.Descendants("item")
.Where(x => x.Attributes("name").ToString().StartsWith("items.names.id"))
.Select(item => new Item
{
Id = (string)item.Attribute("name"),
Name = (string)item.Element("property")
}).ToList();
However, the list is empty. What am I missing here?
Using LINQ to XML:
XDocument xDoc = XDocument.Load(filepath);
var myList = xDoc.Descendants("item").Select(item => new Item {
Id = (string)item.Attribute("name"),
Property = (string)item.Element("property")
}).ToList();
You can use LinqToXml to directly query the XML, or deserialize it and use LINQ to object. If you choose to deserialize I suggest to start from the schema and generate the classes representing your datamodel with xsd.exe. If you don't have the schema of your xml, even xsd.exe can infer one from an example xml file, but you probably need to fine tune the result.
Try this one XElement root = XElement.Parse("your file name");
var items textSegs =(from item in root.Descendants("item")
select item).ToList();
Now iterate over list and store it
The below is a way of getting information from xml using Xdocument.
string input = "<Your xml>";
Xdocument doc = XDocument.Parse(input);
var data = doc.Descendants("item");
List<Items> itemsList = new List<Items>();
foreach(var item in data)
{
string itemname= item.Element("item").Value;
string property = item.Element("property").Value;
itemsList.Add(new item(itemname, property));
}
I'm guessing you want the code given how your question is phrased.. also I'm assuming the real XML is very simplistic as well.
var items = from item in doc.Descendants("item")
select new Item()
{
Id = item.Attributes("name").First().Value,
Property = item.Elements().First().Value,
};
Just ensure that your xml is loaded into doc. You can load the xml in two ways:
// By a string with xml
var doc = XDocument.Parse(aStringWithXml);
// or by loading from uri (file)
var doc = XDocuemnt.Load(aStringWhichIsAFile);

LINQ to XML select

I have two xml documents with some elements like
doc1
<Item id="22"/>
<Item id="33"/>
<Item id="44"/>
...
doc2
<Item id="33"/>
<Item id="44"/>
<Item id="66"/>
<Item id="88"/>
...
I need a query to select
only those elements from doc1 that are missing in doc2 ignoring other doc2 elements.
In this case the result will be:
<Item id="22"/>
How do I do that?
Basically, you create a List with all ids from the second list, and check for each item of doc1 if it is in the list.
Performance wise, I think it isnt the best choice - but it should work
var qry = from item in doc1.Descendants("Item")
where
!(from item2 in doc2.Descendants("Item")
select item2.Attribute("id"),Value
).ToList().Contains(item.Attribute("id").Value)
select item;
In the linq-statement above, I think the list of ids is created for every element in doc1. Better option would be to create the list first and then use the list in the next statement:
List<string> items = (from item2 in doc2.Descendants("Item")
select item2.Attribute("id").Value
).ToList();
var qry = from item in doc1.Descendants("Item")
where !items.Contains(item.Attribute("id").Value)
select item;
Probably something along the lines of
doc1.Where(i1=>doc2.All(i2 => i2.id != i1.id))
could get you there.
HOWEVER, this is performing a subquery on doc2 for each element in doc1. Make sure they are small!
The easiest way is to use the ExceptedBy method from the MoreLinq library. Assuming the Item elements are directly under the root element:
var doc1 = XDocument.Load("doc1.xml");
var doc2 = XDocument.Load("doc2.xml");
var doc1Elements = doc1.Root.Elements("Item");
var doc2Elements = doc2.Root.Elements("Item");
var diff = doc1Elements.ExceptBy(doc2Elements, e => e.Attribute("id").Value);

Bind XML data to a Dropdownlist c#

I am trying to bind XML data to Dropdownlist
XElement xDoc = XElement.Parse(QContent.OuterXml);
Here is what my xDoc contains
<root xmlns="">
<item value="-1" text="Select" />
<item value="1" text="$30,000" />
<item value="2" text="$50,000" />
</root>
Query to extract the data into a Listitem :
var query = from xEle in xDoc.Descendants("root")
select new ListItem(xEle.Attribute("value").Value , xEle.Attribute("text").Value);
This yields no results. Please advice.
Thanks in advance
BB
Put your XML file inside App_Data or anywhere in program and in MapPath.
Assign their path and also assign the name of your first XML column in the last line.
Here I'm using "code" because it is my first XML column and country is the name of the column I want to show in my dropdownlist.
private void BindCountry()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~//App_Data//countries.xml"));
foreach (XmlNode node in doc.SelectNodes("//country"))
{
ddlcountry.Items.Add(new ListItem(node.InnerText, node.Attributes["code"].InnerText));
}
}
You can change your LINQ query to the following which will return all the nodes under root and return new items with the value/text pairs that you are trying to bind to.
var query = from xEle in xDoc.Descendants()
select new {value = xEle.Attribute("value").Value , text = xEle.Attribute("text").Value};
Then set up your bindings as follows including collapsing the query to a list.
ddlList.DataValueField = "value";
ddlList.DataTextField = "text";
ddlList.DataSource = query.ToList();
ddlList.DataBind();

Loading duplicate XML attributes using XDocument

I need help loading xml using XDocument. The xml holds the data for a HierarchicalDataTemplate in WPF so each element has the same attributes.
I'm having a newbie problem with how to handle the duplicate attributes Name, image and fileLoc.
I was trying to get something like the code below to work, but as you can see duplicate attributes will not work.
public static List<MenuItem> Load(string MyMenuFile)
{
var mymenu = XDocument.Load(MyMenuFile).Root.Elements("Menu").Select(
x => new MenuItem(
(string)x.Attribute("id"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc"),
(string)x.Element("itemlist"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc"),
(string)x.Element("item"),
(string)x.Attribute("name"),
(string)x.Attribute("image"),
(string)x.Attribute("fileLoc")));
return stationfiles.ToList();
}
Here is the xml:
<Menus>
<Menu id="1" Name="Level1" image="C:\lvl1.jpg" fileLoc="C:\lvl1.xml">
</Menu>
<Menu id="2" Name="Level2" image="C:\lvl2.jpg" >
<itemlist Name="Level2" image="C:\lvl2.jpg" fileLoc="C:\lvl2.xml">
</itemlist>
<itemlist Name="Level3" image="C:\lvl3.jpg">
<item Name="First" image="C:\first.jpg" fileLoc="C:\first.xml"></item>
<item Name="Second" image="C:\second.jpg" fileLoc="C:\second.xml"></item>
<item Name="Third" image="C:\third.jpg" fileLoc="C:\third.xml"></item>
</itemlist>
</Menu>
</Menus>
As you can see, different elements but duplicate attributes. Should I have 3 separate classes, but how would I combine them for the XDocument load? Any help would be great.
This assumes those are elements and attributes directly of MenuItem. What I suspect is that you need read attributes of elements itemslist and items. Not sure how to do it with a single loop. You need to loop through the elements and then loop the attribute so THAT element (not the parent element).
You are not being heirarchical in your processing.
I have adjusted your xml, but here is an example of how you should be processing it:
string xml = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<Menus>
<Menu id=""1"" Name=""Level1 - Alpha"" image=""C:\lvl1.jpg"" fileLoc=""C:\lvl1.xml""/>
<Menu id=""2"" Name=""Level1 - Beta"" image=""C:\lvl2.jpg"" fileLoc=""C:\lvl1.xml"" >
<itemlist Name=""Level2-Gamma"" image=""C:\lvl2.jpg"" fileLoc=""C:\lvl2.xml""/>
<itemlist Name=""Level3-Zeta"" image=""C:\lvl3.jpg"" fileLoc=""C:\lvl1.xml"">
<item Name=""First"" image=""C:\first.jpg"" fileLoc=""C:\first.xml""></item>
<item Name=""Second"" image=""C:\second.jpg"" fileLoc=""C:\second.xml""></item>
<item Name=""Third"" image=""C:\third.jpg"" fileLoc=""C:\third.xml""></item>
</itemlist>
</Menu>
</Menus>";
var xd = XDocument.Parse(xml);
var result =
xd.Descendants("Menu")
.Select (l1 => new
{
Name = l1.Attribute("Name").Value,
Image = l1.Attribute("image").Value,
File = l1.Attribute("fileLoc"),
Children = l1.Descendants("itemlist")
.Select (l2 => new {
Name = l2.Attribute("Name").Value,
Image = l2.Attribute("image").Value,
File = l2.Attribute("fileLoc"),
Children = l2.Descendants("item")
.Select (l3 => new {
Name = l3.Attribute("Name").Value,
Image = l3.Attribute("image").Value,
File = l3.Attribute("fileLoc")
})
})
});
Console.WriteLine (result );
Here is the result as found from linqpad:
See how the data parses out, that is how you need to work with it to get it into the menu structure. There are no duplicate attributes. :-)
HTH

LINQ to XML Newbie Question: Returning More Than One Result

Greetings!
I'm working on wrapping my head around LINQ. If I had some XML such as this loaded into an XDocument object:
<Root>
<GroupA>
<Item attrib1="aaa" attrib2="000" attrib3="true" />
</GroupA>
<GroupB>
<Item attrib1="bbb" attrib2="111" attrib3="true" />
<Item attrib1="ccc" attrib2="222" attrib3="false" />
<Item attrib1="ddd" attrib2="333" attrib3="true" />
</GroupB>
<GroupC>
<Item attrib1="eee" attrib2="444" attrib3="true" />
<Item attrib1="fff" attrib2="555" attrib3="true" />
</GroupC>
</Root>
I'd like to get the attribute values of all of the Item child elements of a Group element. Here's what my query looks like:
var results = from thegroup in l_theDoc.Elements("Root").Elements(groupName)
select new
{
attrib1_val = thegroup.Element("Item").Attribute("attrib1").Value,
attrib2_val = thegroup.Element("Item").Attribute("attrib2").Value,
};
The query works, but if for example the groupName variable contains "GroupB", only one result (the first Item element) is returned instead of three. Am I missing something?
XElement e = XElement.Parse(testStr);
string groupName = "GroupB";
var items = from g in e.Elements(groupName)
from i in g.Elements("Item")
select new {
attr1 = (string)i.Attribute("attrib1"),
attr2 = (string)i.Attribute("attrib2")
};
foreach (var item in items)
{
Console.WriteLine(item.attr1 + ":" + item.attr2);
}
Yes, .Element() only returns the first matching element. You want .Elements() and you need to re-write your query somewhat:
var results = from group in l_theDoc.Root.Elements(groupName)
select new
{
items = from i in group.Elements("Item")
select new
{
attrib1_val = i.Attribute("attrib1").Value,
attrib2_val = i.Attribute("attrib2").Value
}
};
Here's the query method form of the answer:
var items =
e.Elements("GroupB")
.SelectMany(g => g.Elements("Item"))
.Select(i => new {
attr1 = i.Attribute("attrib1").Value,
attr2 = i.Attribute("attrib2").Value,
attr3 = i.Attribute("attrib3").Value
} )
.ToList()
Another possibility is using a where clause:
var groupName = "GroupB";
var results = from theitem in doc.Descendants("Item")
where theitem.Parent.Name == groupName
select new
{
attrib1_val = theitem.Attribute("attrib1").Value,
attrib2_val = theitem.Attribute("attrib2").Value,
};

Categories