I have an XML file which has kind of a similar structure that you can see below:
I would like to select title and subitems using LINQ to XML. The difficulties that I have: sometimes a subitem can be just one and sometimes it can be 20 subitems, and I need to add them to List<string>.
<?xml version="1.0"?>
<items>
<item>
<title>Name of the title</title>
<subitem>Test</subitem>
<subitem1>Test</subitem1>
<subitem2>Test</subitem2>
<subitem3>Test</subitem3>
<subitem4>Test</subitem4>
<subitem5>Test</subitem5>
</item>
<item>
<title>Name of the title</title>
<subitem>Test</subitem>
<subitem1>Test</subitem1>
<subitem2>Test</subitem2>
<subitem3>Test</subitem3>
</item>
<item>
<title>Name of the title</title>
<subitem>Test</subitem>
<subitem1>Test</subitem1>
</item>
</items>
The solution, including getting the titles, is:
XDocument yourXDocument = XDocument.Load(yourXmlFilePath);
IEnumerable<Tuple<XElement, IEnumerable<XElement>>> yourSubItems =
yourXDocument.Root.Descendants()
.Where(xelem => xelem.Name == "title")
.Select(xelem => new Tuple<XElement, IEnumerable<XElement>>(xelem, xelem.Parent.Elements().Where(subelem => subelem.Name.LocalName.StartsWith("subitem")));
XDocument xdoc = XDocument.Load(path_to_xml);
var query = from i in xdoc.Descendants("item")
select new
{
Title = (string)i.Element("title"),
Subitems = i.Elements()
.Where(e => e.Name.LocalName.StartsWith("subitem"))
.Select(e => (string)e)
.ToList()
};
Related
<?xml version="1.0" encoding="UTF-8"?>
<Batch Id="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PurchaseOrders>
<PurchaseOrder id="xx267681">
<Header>
<AccountNumber>999</AccountNumber>
<ShipDate>2/10/2009</ShipDate>
</Header>
<PurchaseOrderDetails>
<Item>
<ItemNumber>yy235240</ItemNumber>
<Quantity>200</Quantity>
</Item>
<Item>
<ItemNumber>yy336820</ItemNumber>
<Quantity>3</Quantity>
</Item>
</PurchaseOrderDetails>
</PurchaseOrder>
<PurchaseOrder id="zz267456">
<Header>
<AccountNumber>123</AccountNumber>
<ShipDate>2/10/2009</ShipDate>
</Header>
<PurchaseOrderDetails>
<Item>
<ItemNumber>nn235240</ItemNumber>
<Quantity>200</Quantity>
</Item>
</PurchaseOrderDetails>
</PurchaseOrder>
</PurchaseOrders>
</Batch>
Attached above is the XML file I am trying to parse. My current C# code find all items in the XML file and assigns it against the PO#. But the recent XML file I got to know that there can be multiple PO# in the same XML file and hence I now need to find only those items matching to that PO#.
So in above example, PONumber with xx267681 has 2 items whereas 2nd PO has only item.
Here is what I tried so far.
try
{
ArrayList ItemsInFeed = new ArrayList();
XDocument xDoc = XDocument.Load(fileName);
foreach (var node in xDoc.Descendants("PurchaseOrder"))
{
poID = node.Attribute("id").Value;
}
foreach (var node in xDoc.Descendants("Item"))
{
Items itemRcd = new Items();
itemRcd.ItemNr = node.Descendants("ItemNumber")?.First().Value;
ItemsInFeed.Add(itemRcd);
}
if (ItemsInFeed.Count > 0)
{
// Do other logic based on the items linked to each PO#.
// Issue found : So far each XML file has one PO#, but latest XML file received has more than PO# and underlying items.
ItemsInFeed.Clear();
}
}
catch (Exception ex)
{
//Catch exception here
}
Try following :
XDocument xDoc = XDocument.Load(fileName);
var results = xDoc.Descendants("PurchaseOrder").Select(x => new
{
poID = (string)x.Attribute("id"),
items = x.Descendants("Item").Select(y => new
{
itemNumber = (string)y.Element("ItemNumber"),
quantity = (int)y.Element("Quantity")
}).ToList()
}).ToList();
I have the following XML dataset
<?xml version="1.0" ?>
<productCatalog>
<catalogName>Freeman and Freeman Unique Catalog 2010</catalogName>
<expiryDate>2012-01-01</expiryDate>
<products>
<product id="1001">
<productName>Gourmet Coffee</productName>
<description>The finest beans from rare Chillean plantations.</description>
<productPrice>0.99</productPrice>
<inStock>true</inStock>
<category id ="100">
<name>Latin Breakfast</name>
<description>International Range</description>
<subcategory id ="SUB1000">
<name>Energy</name>
<description>blah blah</description>
</subcategory>
</category>
</product>
<product id="1002">
<productName>Blue China Tea Pot</productName>
<description>A trendy update for tea drinkers.</description>
<productPrice>102.99</productPrice>
<inStock>true</inStock>
<category id ="200">
<name>Asian Breakfast</name>
<description>Asian Range</description>
<subcategory id ="SUB1000">
<name>Organic</name>
<description>healthy organic food for a longer life</description>
</subcategory>
</category>
</product>
<product id="1002">
<productName>Blue China Tea Pot</productName>
<description>A trendy update for tea drinkers.</description>
<productPrice>102.99</productPrice>
<inStock>true</inStock>
<category id ="300">
<name>Italian Breakfast</name>
<description>Roman Breakfast</description>
<subcategory id ="SUB2000">
<name>italian</name>
<description>Roman sttyle breakfast</description>
</subcategory>
</category>
</product>
</products>
</productCatalog>
i want to get all products who's with subcategory id = "SUB1000"
i have written the code
public static void ProductsFilteredBySubCategory(string path) {
XElement root = XElement.Load(path);
IEnumerable<XElement> productElems = root.Element("products").Elements().Where(e => e.Name == "product" ).Select(s => s);
IEnumerable<XElement> subcats;
foreach (var item in productElems){
Console.WriteLine( item.Element("category").Elements().Where(e => e.Name == "subcategory").Select(s => s.Name) );
}
}
but the print statement in the foreach does not seems to have the products that was filtered, How do i filter the products by the desired subcategory id? Maybe i'm doing this in the incorrect way...
You're going about it in somewhat of a roundabout way. Here is how I would structure this:
XDocument document = XDocument.Load(path);
var elements = document.Descendants("subcategory")
.Where(i => (string)i.Attribute("id") == "SUB1000")
.Select(i => i.Parent.Parent);
foreach(var element in elements)
{
Console.WriteLine(element);
}
Granted, here I am not looking at specific entity types, but just assuming that the IDs are unique for what you are trying to find, that will extract the right elements.
Descendants can be useful in this case.
var document = XDocument.Load(path);
var products = document.Descendants("product")
.Where(product => product.Descendants("subcategory")
.Any(sub => sub.Attributes("id")
.Any(id => id.Value == "SUB1000")));
foreach(var product in products)
{
var subId = product.Attributes("id").Select(id => id.Value).FirstOrDefault();
Console.WriteLine($"Product: {subId}");
}
You can use the below code to get the product
var document = XDocument.Load("pathtothexml");
var coll = document.Descendants("subcategory").Where(s => s.Attribute("id").Value.Equals("SUB1000")).Ancestors("product");
I have some XML structured like this:
<form>
<section-1>
<item-1>
<value />
</item-1>
<item-2>
<value />
</item-2>
</section-1>
<section-2>
<item-3>
<value />
</item-3>
<item-4>
<value />
</item-4>
</section-2>
</form>
...and want to turn it into something sane like this:
<form>
<items>
<item id="1">
<value/>
</item>
<item id="2">
<value/>
</item>
<item id="3">
<value/>
</item>
<item id="4">
<value/>
</item>
</items>
</form>
I am struggling to turn the old XML into an array or object of values. Once in the new format I'd be able to do the following:
XDocument foo = XDocument.Load(form.xml);
var items = foo.Descendants("item")
.Select(i => new Item
{
value = i.Element("value").Value
});
...but in the current mess the xml is in can I wildcard the descendants selector?
var items = foo.Descendants("item"*)
...or something? I tried to follow this question's answer but failed to adapt it to my purpose.
Ah-ha! It did click in the end. If I leave the descendants selector blank and add in a where statement along the lines of what's in this question's answer
.Where(d => d.Name.ToString().StartsWith("item-"))
Then we get:
XDocument foo = XDocument.Load(form.xml);
var items = foo.Descendants()
.Where(d => d.Name.ToString().StartsWith("item-"))
.Select(i => new Item
{
value = i.Element("value").Value
});
...and I'm now able to iterate through those values while outputting the new XML format. Happiness.
This question already has an answer here:
How to sort XML in LINQ C# by an attribute value? Also MVC
(1 answer)
Closed 7 years ago.
I have the following xml file.(sample) .. I need to sort the 'invoice' nodes by the attribute 'InvcDate'. Is this even possible in Linq? Any help would be much appreciated.
I have been trying for some time however I don't have much experience with xml and and am a relative newcomer to programming, so I would be very grateful for any help at all.
<?xml version="1.0" encoding="utf-8"?>
<Server>
<Name>AlignServer</Name>
<Params>
<marketNo>MT</marketNo>
<dateFrom>2015-01-06</dateFrom>
<dateTo>2015-01-09</dateTo>
<Sales>
<invoices>
<invoice>
<header>
<InvoiceNum>22947</InvoiceNum>
<InvcDate>2015/01/07-110104</InvcDate>
</header>
<item>
<SKU>6595456987453</SKU>
<Qty>-1</Qty>
</item>
</invoice>
<invoice>
<header>
<InvoiceNum>23056</InvoiceNum>
<InvcDate>2015/01/08-020627</InvcDate>
</header>
<item>
<SKU>9845256242255</SKU>
<Qty>-1</Qty>
</item>
</invoice>
<invoice>
<header>
<InvoiceNum>22899</InvoiceNum>
<InvcDate>2015/01/06-094505</InvcDate>
</header>
<item>
<SKU>5454256565452</SKU>
<Qty>-1</Qty>
</item>
<item>
<SKU>11111165454130</SKU>
<Qty>4</Qty>
</item>
</invoice>
</invoices>
</Sales>
</Params>
</Server>
I have tried
XElement root = XElement.Load("C:\\xmlsort\\test.xml");
XElement[] sortedTables = root.Elements("invoices").OrderBy(t => (Datetime)t.Element("invdate")).ToArray();
root.ReplaceAll(sortedTables);
root.Save("C:\\xmlsort\\test.xml");
What I have done so far - with suggestion from #ec8or and seems to work but still open to suggestions:
XElement root = XElement.Load("C:\\xmlsort\\test.xml");
var invoices = from p in root.Descendants("invoice")
orderby DateTime.ParseExact(p.Element("header").Element("InvcDate").Value, "yyyy/MM/dd-hhmmss", CultureInfo.InvariantCulture)
select p;
XElement[] sortedTables = invoices.ToArray();
root.ReplaceAll(sortedTables);
root.Save("C:\\xmlsort\\output.xml");
Read you XML in a XElement:
XElement element = XElement.Load("doc.xml");
Query you XML data:
var invoices = from p in element.Descendants ("invoice")
orderby DateTime.ParseExact(p.Element("header").Element("InvcDate").Value, "yyyy/MM/dd-hhmmss", CultureInfo.InvariantCulture)
select p;
Print it to the console:
foreach (var invoice in invoices) {
Console.WriteLine (invoice.ToString ());
}
EDIT
Answer to your question in comments.
XDocument doc = XDocument.Load ("data.xml");
Select all parent node:
var baseElement = doc.XPathSelectElement ("/Server/Params/Sales/invoices");
sort the inner nodes:
var sortedElements = baseElement.Elements ()
.OrderBy (e => (DateTime)e.XPathSelectElement("header/InvoiceNum"))
.ToList ();
replace the current content with the sortet content:
baseElement.ReplaceAll (sortedElements);
doc.Save ("out.xml");
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.