How to retrieve attributes value with multiple level using Xml Linq? - c#

I have a xml like this :
<?xml version="1.0" encoding="iso-8859-1" ?>
<Menu>
<Group Flow="Horizontal">
<Item Text="Basket" >
<Item Text="Favorites">
<Item Text="Add" Roles="SuperAdmin">
<Item Text="Evaluation" Roles="Admin">
</Item>
<Item Text="Titularisation" Roles="users,Admin">
</Item>
</Item>
</Group>
</Menu>
and I want to retrieve all attributes "Roles".
I tried this :
XElement rootElement = XElement.Load(#"C:\Users\TTM\Desktop\GeneralMenu.xml");
XElement menuElt = rootElement.Element("Group");
var itemsAdd = from el in menuElt.Descendants("Item")
where (string)el.Attribute("Text") == "Add"
select el;
foreach (var item in itemsAdd)
{
Console.WriteLine(item.Attribute("Roles").Value);
}
Console.Read();
but I get only the "Roles" of this balise:
<Item Text="Add" Roles="SuperAdmin">

Try this:-
XDocument xdoc = XDocument.Load(XMLFile);
var itemAdd = xdoc.Descendants("Item")
.Where(x => (string)x.Attribute("Text") == "Add")
.Select(x => new
{
Roles = (string)x.Attribute("Roles"),
ChildRoles = x.Descendants("Item")
.Select(z => (string)z.Attribute("Roles")).ToList()
});
Here, Roles will contain the Roles from your parent node, and ChildRoles will hold all the Roles present in the descendants of parent where Text is Add.
You can then access them using foreach loop:-
foreach (var item in itemAdd)
{
Console.WriteLine(item.Roles);
foreach (var childRoles in item.ChildRoles)
{
Console.WriteLine(childRoles);
}
}
If you want all the roles to be fetched in a single property then you can fetch it like this:-
Roles = x.Descendants("Item").Select(z => (string)z.Attribute("Roles"))
.Concat(new List<string> { (string)x.Attribute("Roles") }).ToList()

Related

Parse XML file nodes and store it to a Dictionary

<?xml version="1.0" encoding="utf-8" ?>
<LanguagePacks>
<Language name = "EN">
<item key="play" value="play"/>
<item key="pause" value="pause"/>
<item key="resume" value="resume"/>
<item key="all" value="all"/>
<item key="songs" value="songs"/>
<item key="song" value="song"/>
<item key="skip" value="skip"/>
<item key="next" value="next"/>
<item key="previous" value="previous"/>
<item key="number" value="number"/>
<item key="album" value="album"/>
</Language>
<Language name = "DE">
<item key="play" value="spiel"/>
<item key="pause" value="pause"/>
<item key="resume" value="resume"/>
<item key="all" value="alle"/>
<item key="songs" value="lieder"/>
<item key="song" value="lied"/>
<item key="skip" value="skipp"/>
<item key="next" value="nachste"/>
<item key="previous" value="vorheriger"/>
<item key="number" value="nummer"/>
<item key="album" value="alben"/>
</Language>
</LanguagePacks>
I want to parse the above xml file for Language name == "EN" and store the key value pairs in a dictionary. The code I am trying in below.. But error shows item with same key is already been added. Please help.
XDocument doc = XDocument.Load($"{path}");
var output = doc.Element("LanguagePacks")
.Descendants().Where(r=>(string)r.Attribute("name").Value == "EN")
.Descendants()
.ToDictionary(k => k.Name, v => v.Value);
.ToDictionary(k => k.Name, v => v.Value);
k is an item, if you want your Dictionary like
[0] = {[play, play]}
[1] = {[pause, pause]}
I think for each item, you access the attribute to get value
var output = doc.Element("LanguagePacks")
.Descendants().Where(r=>(string)r.Attribute("name").Value == "EN")
.Descendants() // list of item
.ToDictionary(k => k.Attribute("key").Value, v => k.Attribute("value").Value);
ToDictionary(k => k.Name, v => v.Value) will not work the way you want, because the Name and Value properties are the node name (item) and node contents (nothing), not the xml attributes "key" and "value".
You also should not use Descendants when you actually only want to loop through elements on one level.
Running your code on your example XML gives a NullReferenceException, because some descendants of <LanguagePacks> does not have a name attribute, and you assume they all do.
This code should do what you want. Note that this will also crash if you have duplicate keys, or if an <item /> element is missing the key or value attributes.
doc.Element("LanguagePacks")
.Elements("Language")
.Where(x => x.Attribute("name")?.Value == "EN")
.Elements("item")
.ToDictionary(x => x.Attribute("key").Value, x => x.Attribute("value").Value)
.Dump();
In
.ToDictionary(k => k.Name, v => v.Value);
k.Name will always be item, not the value of key="...".
And I guess that v.Value will always be "", not entirely sure.

Get XML nodes attributes and set as properties of List<myType>?

Example XML:
<Root>
<Product value="Candy">
<Item value="Gum" price="1.00"/>
<Item value="Mints" price="0.50"/>
</Product>
</Root>
Let's say I have a class with properties:
public class CandyItems
{
public string Value{get; set;}
public string Price{get; set;}
}
And within my main program class, I have a list:
var Candies = new List<CandyItems>;
I am struggling with a concise way to populate the Candies list, using LINQ.
I could do it in steps, like this:
//Get list of Items within <Product value="Candy">
XElement tempCandies = XDocument.Load("file.xml").Root.Elements("Product").Single(c => c.Attributes("value") == "Candy").Descendants("Item");
//Loop through the elements
foreach(var item in tempCandies){
Candies.Add(new CandyItems{Value = item.Attributes("value"), Price = item.Attributes("price")});
}
But it seems like I could do this more concisely with pure LINQ somehow. Or is there another recommended method?
Try this:-
XDocument xdoc = XDocument.Load(#"Path\Candies.xml");
List<CandyItems> Candies = xdoc.Descendants("Item")
.Select(x => new CandyItems
{
Value = (string)x.Attribute("value"),
Price = (string)x.Attribute("price")
}).ToList();
Although, you have not mentioned but if you want to just fetch Candies and your XML may contain other products too like:-
<Root>
<Product value="Candy">
<Item value="Gum" price="1.00"/>
<Item value="Mints" price="0.50"/>
</Product>
<Product value="Chocolate">
<Item value="MilkChocolate" price="7.00"/>
<Item value="DarkChocolate" price="10.50"/>
</Product>
</Root>
Then you can apply a filter to fetch only Candy products like this:-
List<CandyItems> Candies = xdoc.Descendants("Item")
.Where(x => (string)x.Parent.Attribute("value") == "Candy")
.Select(x => new CandyItems
{
Value = (string)x.Attribute("value"),
Price = (string)x.Attribute("price")
}).ToList();
How about something like this (after loading the document):
var candies =
xdoc.Root.Elements("Product")
.Where(p => p.Attribute("value").Value == "Candy")
.SelectMany(p => p.Descendants("Item").Select(i => new CandyItems {
Value = i.Attribute("value").Value,
Price = i.Attribute("price").Value }));
Note: any and all error handling omitted.

How find element in xdocument and read next elements after them

So i have next xml document:
<Items>
<Item>
<ID>123</ID>
<Name>Super Item</Name>
<Count>1</Count>
<Price>45</Price>
</Item>
<Item>
<ID>456</ID>
<Name>not super Item</Name>
<Count>10</Count>
<Price>5</Price>
</Item>
<Item>
<ID>789</ID>
<Name>Simple Item</Name>
<Count>6</Count>
<Price>10</Price>
</Item>
</Items>
So how can i find needed item by ID and read next values? Thanks in advance.
code:
XDocument doc = XDocument.Load (filePath);
foreach (var item in doc.Descendants("ID"))
{
if ((string)item.Element("ID") == "789")
{
How to read Name "Simple Item"?
How to read Count "6"?
How to read Price "10"?
}
}
By what you are asking you could formatt your xml like this:
<Items>
<Item id="123">
<Name>Super Item</Name>
<Count>1</Count>
<Price>45</Price>
</Item>
<Item id="456">
<Name>not super Item</Name>
<Count>10</Count>
<Price>5</Price>
</Item>
<Item id="789">
<Name>Simple Item</Name>
<Count>6</Count>
<Price>10</Price>
</Item>
</Items>
Then in code:
int yourId = 456;
XDocument doc = XDocument.Load("test.xml");
var result = from el in doc.Root.Elements("Item")
where el.Attribute("id").Value == yourId.ToString()
select el;
The id here is an attribute.And for reading its values 2 ways:
//1º
foreach (var item in result.Elements())
{
Console.WriteLine(item.Name + " = " + item.Value);
}
//2º - will print the element
Console.WriteLine(result);
It depends on what you want to do when you find those values. Here is a general method using a foreach loop to find the item with the specified ID and returning it's name:
private string GetItemName(string _id)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("myXmlFile.xml");
foreach (XmlNode item in xDoc.SelectNodes("/Items/Item"))
{
if (item.SelectSingleNode("ID").InnerText == _id)
{
// we found the item! Now what do we do?
return item.SelectSingleNode("Name").InnerText;
}
}
return null;
}

Find parent with defined attribute wuth LINQ

I have variable XElement content =
"...
<item text="Name-1" id="1" segment="1" secret-id="Find-Me">
<item text="Name-1-1" id="1.1">
<item text="Name-1-1-1" id="1.1.1" />
<item text="Name-1-1-2" id="1.1.2" />
</item>
</item>
..."
I have also an item with id = 1.1.2
I need to find the first parent of this item which has attribute segment="1" and get its secret-id
How to do this with LINQ?
You mean the "closest ancestor"?
string secretId = element.Ancestors()
.Where(x => (string) x.Attribute("segment") == "1")
.Select(x => (string) x.Attribute("secret-id"))
.FirstOrDefault();
The result will be null if either:
There are no ancestors with an attribute segment="1"
The element with that attribute has no secret-id attribute.
Like this:
item.Ancestors().Select(p => p.Attribute("secret-id")).First(a => a != null)

LINQ to XML Selecting Child Elements

I am trying to extract information from an XML file into an object using LINQ to XML. Although I can return the document and section Id attributes I cannot get access to the Items for each section element, it returns an IEnumerable of all the items in the document. I know this is correct as I’m calling Descendants but am struggling to get it to return only the child items of each section element. Can anybody help?
XML Document
<root>
<document id="1">
<section id="1.1">
<item id="1.1.1"></item>
<item id="1.1.2"></item>
<item id="1.1.3"></item>
</section>
<section id="1.2">
<item id="1.2.1"></item>
<item id="1.2.2"></item>
</section>
</document>
</root>
LINQ Query
XElement documentRoot = XElement.Load("document.xml");
var documents = (from docs in documentRoot.Descendants("document")
select new
{
Id = (string) docs.Attribute("id"),
Sections = docs.Elements("section"),
Items = docs.Elements("section").Elements("item")
}).ToList();
foreach(var doc in documents)
{
foreach(var section in doc.Sections)
{
Console.WriteLine("SectionId: " + section.Attribute("Id"));
foreach(var item in doc.Items)
{
Console.WriteLine("ItemId: " + section.Attribute("Id"));
}
}
}
You got some small typos, on attribute Id and at item loop. But if you're trying to get all section items into that items collection, you're wrong at design level, as Items should be a Section property, not Document (so you'll need to query your XML twice).
Or, you can to do something like:
var documents =
(from docs in documentRoot.Descendants("document")
select new
{
Id = (string) docs.Attribute("id"),
Sections = docs.Elements("section")
}).ToList();
foreach (var doc in documents)
{
foreach (var section in doc.Sections)
{
Console.WriteLine("SectionId: " + section.Attribute("id"));
foreach (var item in section.Elements("item"))
{
Console.WriteLine("ItemId: " + item.Attribute("id"));
}
}
}
Output:
SectionId: id="1.1"
ItemId: id="1.1.1"
ItemId: id="1.1.2"
ItemId: id="1.1.3"
SectionId: id="1.2"
ItemId: id="1.2.1"
ItemId: id="1.2.2"
Do you want a flat structure ?!?! (from LinqPad)
XElement documentRoot = XElement.Parse (
#"<root>
<document id='1'>
<section id='1.1'>
<item id='1.1.1'></item>
<item id='1.1.2'></item>
<item id='1.1.3'></item>
</section>
<section id='1.2'>
<item id='1.2.1'></item>
<item id='1.2.2'></item>
</section>
</document>
</root>
");
var documents = (from docs in documentRoot.Descendants("section")
select new
{
SectionId = (string) docs.Attribute("id"),
Items = docs.Elements("item")
});
documents.Dump();
This gives 2 SectionId items that containes XElements with related items.

Categories

Resources