linq-to-xml

Using LINQ to XML to Process XML in Multiple Namespaces

早过忘川 提交于 2019-12-10 23:46:47
问题 I'm trying to parse results from the YouTube API. I'm getting the results correctly as a string, but am unable to parse it correctly. I followed suggestions on a previous thread, but am not getting any results. My sample code is: string response = youtubeService.GetSearchResults(search.Term, "published", 1, 50); XDocument xDoc = XDocument.Parse(response, LoadOptions.SetLineInfo); var list = xDoc.Descendants("entry").ToList(); var entries = from entry in xDoc.Descendants("entry") select new {

XElement attribute sorting

ε祈祈猫儿з 提交于 2019-12-10 23:39:17
问题 I have a XML file like that: <Users> <User> <Adress Name="bbbb"/> <Adress Name="aaaa" /> </User> </Users> I want to sort User element's nodes in ascending order. How can I order Adress elements? Thank you for your help. 回答1: If node is your user node: node.Elements("Adress").OrderBy(e=>e.Attribute("Name").Value) 回答2: Are you merely wanting to work with the XML objects in memory or are you looking to store the sorted results back in a file? This code shows reordering the elements within an

Searching for XElement with attribute name that contain hyphens/dashes

喜你入骨 提交于 2019-12-10 22:27:43
问题 I wrote some code in VB.Net a while ago that is using XElement, XDocument, etc... to store and manipulate HTML. Some of the HTML makes use of attribute names that contain a hyphen/dash (-). I encountered issues using LinqToXml to search for XElements by these attributes. Back then I found an article (can't find it now) that indicated the solution in VB.net was to use syntax like this: Dim rootElement as XElement = GetARootXElement() Dim query = From p In rootElement.<div> Where p.@<data-qid>

Unable to cast object of type 'WhereSelectEnumerableIterator

梦想与她 提交于 2019-12-10 22:05:51
问题 I am trying to cast my xml result to an IQueryable which gives this error: An unhandled exception of type 'System.InvalidCastException' occurred in weatherxml.exe Additional information: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Linq.IGrouping`2[System.String,System.Xml.Linq.XElement],System.Linq.IGrouping`2[System.String,System.Xml.Linq.XElement]]' to type 'System.Linq.IQueryable`1[weatherxml.Station]'. How can I convert this to get the right stations conversion?

XDocument or XmlDocument to JSON with C#

喜你入骨 提交于 2019-12-10 22:05:08
问题 I have this XML which is great: <Products> <Product ProductCode="C1010" CategoryName="Coins" /> <Product ProductCode="C1012" CategoryName="Coins" /> <Product ProductCode="C1013" CategoryName="Coins" /> </Products> but it outputs to this JSON: {"Products":{"Product":[{"@ProductCode":"C1010","@CategoryName":"Coins"}, {"@ProductCode":"C1012","@CategoryName":"Coins"}, {"@ProductCode":"C1013","@CategoryName":"Coins"}]}} I would like no 'Product' sublevel in my json because all three lines are a

How to query XElement with two namespaces

邮差的信 提交于 2019-12-10 20:09:23
问题 I'm trying to find the inner text value of an element using LINQ-to-XML (an XElement object). I make my service call and get an XML response back that I've successfully loaded into an XElement object. I want to extract the inner text of one of the elements - however, every time I try to do this, I get a null result. I feel like I'm missing something super-simple, but I'm fairly new to LINQ-to-XML. Any help is appreciated. I'm trying to get the inner text value of the StatusInfo/Status element

Reading and using an XML file as a database - Windows Phone 7 app

自古美人都是妖i 提交于 2019-12-10 18:24:16
问题 I have an app which should read the data from an xml file and then use that data. How can I import an xml file in my app (what's the code for that) and how can I use the data from that xml file? Here's an example of the xml database I use: <Data> <Animals> <A> <word>Ant</word> <word>Aardwark</word> </A> <B> <word>Bear</word> <word>Boa</word> </B> </Animals> </Data> Also I tried this XDocument loadedData = XDocument.Load("Data.xml"); to read the data from the xml file but didn't work. Also the

How can I save the XML contents of an XDocument as an .xml file?

不想你离开。 提交于 2019-12-10 17:47:18
问题 I have an XDocument class with the XML contents already made. I basically want to open a SaveFileDialog, have the user choose a folder (not a file) in which to save the contents as an .xml file. I'm having some difficulty doing so: a) How can I use the SaveFileDialog to prompt the user to select a folder? I've only been able to use it to get a user to select a file. b) How do I extract the chosen path from SaveFileDialog? c) Once I have the path, how can I save the contents of the XDocument?

How to load XML file located inside the folder of the application in window phone 7?

时光怂恿深爱的人放手 提交于 2019-12-10 17:33:16
问题 I am developing window phone 7 application. I am new to the window phone 7 application. I have added XML file in my project by right clicking the project & selecting the Add -> New Item. I can then easily load the XML file in my application by using the following code IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication(); XDocument doc = null; IsolatedStorageFileStream isfStream = null; if (isfData.FileExists(strXMLFile)) { isfStream = new IsolatedStorageFileStream

How to iterate through an XDocument, getting complete XML structure, object by object?

核能气质少年 提交于 2019-12-10 17:12:27
问题 I have an XmlDocument which I can traverse with XmlNode or convert it to a XDocument and traverse it via LINQ. <Dataset> <Person> <PayrollNumber>1234567</PayrollNumber> <Surname>Smith-Rodrigez</Surname> <Name>John-Jaime-Winston Junior</Name> <Skills> <Skill>ICP</Skill> <Skill>R</Skill> </Skills> <HomePhone>08 8888 8888</HomePhone> <MobilePhone>041 888 999</MobilePhone> <Email>curly@stooge.com</Email> </Person> <Person> <PayrollNumber>12342567</PayrollNumber> <Surname>Smith-Rodrigez</Surname>