Hi because of a misunderstanding I want to ask my question again.
I have the following XML structure:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<root>
<Item>
<taxids>
<string>330</string>
<string>374</string>
<string>723</string>
<string>1087</string>
<string>1118</string>
<string>1121</string>
</taxids>
</Item>
</root>
</xml>
I need to get all the string nodes from the xml file to a string variable.
I want to get a string like this:
<taxids><string>330</string><string>374</string><string>723</string><string>1087</string><string>1118</string><string>1121</string></taxids>
My linq to xml:
var query = from ip in doc.XPathSelectElements("xml/root/Item")
select ip.XPathSelectElement("taxids").ToString();
But I am getting the following in one row of the variable query:
"System.Xml.XPath.XPathEvaluator+<EvaluateIterator>d__0`1[System.Xml.Linq.XElement]"
Is this possible?
Thanks!
Try this:
var result = doc.Element("xml")
.Element("root")
.Element("Item")
.Element("taxids")
.ToString(SaveOptions.DisableFormatting);
// result == "<taxids><string>330</string><string>374</string> ... </taxids>"
Related
I have an xml as an value of an element inside a Main xml. I want to scrub off or delete a node within the inner xml. How do I achieve that ?
For removing a node in main xml I am doing
var result = doc.Descendants("node1").Where(x => x.Attribute("id").Value == "002");
if (result != null)
{
result.Remove();
}
Here is my XML :
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1>id="001" version="1.0"</node1>
<node2>id="002" version="1.0"</node1>
<report>raw = "<response = "10"><innerxml><prod>date = "18082016" name="pqr"</prod><seg1>id="002" name = "sqs"</seg1></innerxml></response>"</report>
</root>
Your code is correct but your xml is not. the XML should be like:
<?xml version="1.0" encoding="utf-16"?>
<root>
<node1 id="001" version="1.0"></node1>
<node2 id="002" version="1.0"></node2>
</root>
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.
I want a query that will return a IEnumerable of string and inside that have
'as.m3'
'as.m4'
I have tried xDoc.Elements("moduleid") and xDoc.Descendents("moduleid")
with no luck
<?xml version="1.0" encoding="UTF-8">
<root>
<code>M11088MUBWWLSRSV9LTJBH81QT</code>
<moduleid>as.m3</moduleid>
<moduleid>as.m4</moduleid>
</root>
Use:
xDoc.Descendants("moduleid").Select(x => (string)x);
Or:
xDoc.Root.Elements("moduleid").Select(x => (string)x);
The XML dataset file after I add it to a DataSet looks like this:
<?xml version="1.0" standalone="yes"?>
<root>
<Email></Email>
<FirstName></FirstName>
<LastName></LastName>
<Addresses>
<item>
<Address1></Address1>
</item>
<item>
<Address1></Address1>
</item>
</Addresses>
<Friends>
<item>
<Name></Name>
</item>
<item>
<Name></Name>
</item>
</Friends>
</root>
I am having issues accessing the Address1 field or the Name field. I can loop through the Addresses or Friends tables but that doesn't help me since the data I want is wrapped one more level down.
I tried this:
foreach (DataRow ar in ds.Tables["Addresses"].Rows)
{
DataRow[] orderDetails = ar.GetChildRows("item");
}
But no success.
Help appreciated.
Thanks
using linq to XML
public static XDocument GetXDocument()
{
XDocument mydata = XDocument.Parse("<?xml version=\"1.0\" standalone=\"yes\"?><root><Email></Email><FirstName></FirstName><LastName></LastName><Addresses><item><Address1>TestData</Address1></item><item><Address1></Address1></item></Addresses> <Friends> <item> <Name></Name> </item><item><Name></Name></item></Friends></root>");
return mydata;
}
this gets the data as a XDocument and this is how you deal with the data
public void OutputAddress()
{
XDocument data = xmlData.GetXDocument();
string Expected = "TestData";
var result = from
addesses in data.Element("root").Elements("Addresses")
where
addesses.Element("item").Element("Address1").Value != string.Empty
select addesses.Element("item").Element("Address1").Value;
foreach (string address1 in result)
{
Console.Write(address1);
}
}
I suggest you use an object wrapper whenever working with XML. This is very eashy to do, I suggest you have a look at this blog:
http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx
Which is aimed at settings XML files however it still applies here.
Thanks
Guido Tapia