linq-to-xml

XDocument : is it possible to force the load of a malformed XML file?

孤人 提交于 2019-12-19 20:00:06
问题 I have a malformed XML file. The root tag is not closed by a tag. The final tag is missing. When I try to load my malformed XML file in C# StreamReader sr = new StreamReader(path); batchFile = XDocument.Load(sr); // Exception I get an exception " Unexpected end of file has occurred. The following elements are not closed: batch. Line 54, position 1. " Is it possible to ignore the close tag or to force the loading? I noticed that all my XML tools ((like XML notepad) ) automaticly fix or ignore

Query Nested XML Elements using Linq-to-XML ASP.Net C#

人盡茶涼 提交于 2019-12-19 11:57:18
问题 in my ASP.Net C# application, I am trying to read a nested XML Elements to an Anonymous type collections. here is the XML sample <MedicationDispensed xmlns="http://www.ncpdp.org/schema/SCRIPT"> <DrugDescription>OXYCODONE W/APAP 5/325 TAB</DrugDescription> <DrugCoded> <ProductCode>00406051205</ProductCode> <ProductCodeQualifier>ND</ProductCodeQualifier> </DrugCoded> <Quantity> <Qualifier>00</Qualifier> <Value>60.0</Value> <CodeListQualifier>87</CodeListQualifier> </Quantity> <DaysSupply>15<

Using Linq to pull from IEnumerable<XElement> and store in data table

不羁岁月 提交于 2019-12-19 11:36:12
问题 I have a datatable that looks like the following public static DataTable SetColumnHeaders(DataTable KeyDataTable) { KeyDataTable.Columns.Add("First_Name", typeof(string)); KeyDataTable.Columns.Add("Last_Name", typeof(string)); KeyDataTable.Columns.Add("Address1", typeof(string)); KeyDataTable.Columns.Add("Address2", typeof(bool)); KeyDataTable.Columns.Add("City", typeof(string)); KeyDataTable.Columns.Add("State", typeof(bool)); KeyDataTable.Columns.Add("Zip", typeof(string)); KeyDataTable

Linq To Xml problems using XElement's method Elements(XName)

百般思念 提交于 2019-12-19 09:27:23
问题 I have a problem using Linq To Xml. A simple code. I have this XML: <?xml version="1.0" encoding="utf-8" ?> <data xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/directory file.xsd"> <contact> <name>aaa</name> <email>email@email.ext</email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> <contact> <name>sss</name> <email>email

Why does XDocument.Parse throw NotSupportedException?

你说的曾经没有我的故事 提交于 2019-12-19 09:26:10
问题 I am trying to parse xml data using XDocument.Parse wchich throws NotSupportedException, just like in topic: Is XDocument.Parse different in Windows Phone 7? and I updated my code according to posted advice, but it still doesn't help. Some time ago I parsed RSS using similar (but simpler) method and that worked just fine. public void sList() { WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; string url = "http://eztv.it"; Uri u = new Uri(url); client.DownloadStringAsync(u)

How do I get a list of child elements from XDocument object?

此生再无相见时 提交于 2019-12-19 09:18:24
问题 I am trying to get all of the "video" elements and their attributes from an XML file that looks like this: <?xml version="1.0" encoding="utf-8" ?> <videos> <video title="video1" path="videos\video1.wma"/> <video title="video2" path="videos\video2.wma"/> <video title="video3" path="videos\video3.wma"/> </videos> The following will only select the root node and all of the children. I would like to get all of the 'video' elements into the IEnumerable. Can someone tell me what I'm doing wrong?

Selecting elements from XML file using LINQ

故事扮演 提交于 2019-12-19 09:04:07
问题 I have this XML structure: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>My Work</name> <Placemark> <name>Main Building</name> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates> </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> <Placemark> <name>Office 1</name> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode>

Linq if/else condition?

老子叫甜甜 提交于 2019-12-19 09:02:37
问题 I know this will probably be a newbie question. Is there a way to choose different search criteria depending on the bool value? Later in the code, I want to loop through the object (alDisabledPrograms). I know the if/else is not correct, I put that in there to show how I'd like that to be handled. I attempted to place this inside a larger if/else condition but was unable to loop through alDisabledPrograms later. Thoughts? var alDisabledPrograms = xlServerRoles.Descendants("ServerRole") if

How set timeout on XDocument.Load(string uri)?

耗尽温柔 提交于 2019-12-19 07:15:46
问题 Is there a way to set a timeout on System.Linq.Xml.XDocument.Load(string uri)? Or should I use the technique described in Implement C# Generic Timeout? 回答1: There is nothing built-in as far as I'm aware. You can fetch the XML content yourself with an instance of WebRequest (which you can set a timeout on) and then pass the XML data directly to the XDocument.Load method. Technically, the most "robust" solution would be to implement XmlResolver yourself which uses a WebRequest in the GetEntity(

How do I find a XML node by path in Linq-to-XML

≡放荡痞女 提交于 2019-12-19 06:46:37
问题 If I get the path to a specific node as a string can I somehow easily find said node by using Linq/Method of the XElement ( or XDocument ). There are so many different types of XML objects it would also be nice if as a added bonus you could point me to a guide on why/how to use different types. EDIT: Ok after being pointed towards XPathSelectElement I'm trying it out so I can give him the right answer I can't quite get it to work though. This is the XML I'm trying out <Product> <Name>SomeName