linq-to-xml

xdocument descendantsnodes vs nodes

好久不见. 提交于 2021-01-29 18:14:18
问题 I'm trying to understand the difference between the extension method, DescendantsnNodes and the method, "Nodes" of the class XDocument. I can see that the DescendantsNodes should return all descendants of this document or element and Nodes should return all child nodes of this document or element. I don't understand what's the difference between "all child nodes" and "all descendants". could someone please clarify this? 回答1: A child node would be a node which is directly "underneath" a

Linq: Elements not working when I have a namespace in XML file

社会主义新天地 提交于 2021-01-28 06:45:44
问题 I found some examples stackoverflow of using a xml to linq parser. My xml has a namespace, so I tried setting that as well (though I would of thought you could read that in a from the file?) Anyway, when I run the c#/linq code it does not recognise the elements in the xml. Unless I remove the xmlns tag from the xml file. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace ConsoleApplication2 { class Program { static void Main

How to get XML element with namespace

泪湿孤枕 提交于 2021-01-28 05:18:43
问题 I have a XML file like this : I want to get all the tag a:entry <?xml version="1.0" encoding="utf-8"?> <a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02"> <a:link rel="next" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&startIndex=50&chunkSize=50&pagingToken=50%7c50&os=8.0.10512.0&cc=US&oc=&lang=vi-VN&hw=469838850&dm=Virtual&oemId=NOKIA&moId=" /> <a:link rel="self" type="application

Remove all comments from XDocument

喜你入骨 提交于 2020-12-05 05:53:21
问题 I am working on reading XDocument. How can I remove all commented lines from XDocument. I have tried with doc.DescendantNodes().Where(x => x.NodeType == XmlNodeType.Comment).Remove(); But this removes only first level nodes with comments and inner level nodes remains as it is. Is there any way to remove all commented lines. I believe there must be!!! ;) Any Solutions. 回答1: Instead of the Where(x => x.NodeType == XmlNodeType.Comment) I would simply use OfType<XComment>() , as in doc

why does the Xdocument give me a utf16 declaration?

有些话、适合烂在心里 提交于 2020-07-31 17:18:57
问题 i'm creating a XDocument like this: XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes")); when i save the document like this ( doc.Save(@"c:\tijd\file2.xml"); ) , i get this: <?xml version="1.0" encoding="utf-8" standalone="yes"?> which is ok. but i want to return the content as xml, and i found the following code: var wr = new StringWriter(); doc.Save(wr); string s = (wr.GetStringBuilder().ToString()); this code works, but then the string 's' starts with this: <?xml

Exception: The XPath expression evaluated to unexpected type System.Xml.Linq.XAttribute

丶灬走出姿态 提交于 2020-06-27 07:04:45
问题 I've an XML file like below: <Employees> <Employee Id="ABC001"> <Name>Prasad 1</Name> <Mobile>9986730630</Mobile> <Address Type="Perminant"> <City>City1</City> <Country>India</Country> </Address> <Address Type="Temporary"> <City>City2</City> <Country>India</Country> </Address> </Employee> Now I want get all Address Type's. I tried like below using XPath and I'm getting exception. var xPathString = @"//Employee/Address/@Type"; doc.XPathSelectElements(xPathString); // doc is XDocument.Load("xml

Exception: The XPath expression evaluated to unexpected type System.Xml.Linq.XAttribute

回眸只為那壹抹淺笑 提交于 2020-06-27 07:04:34
问题 I've an XML file like below: <Employees> <Employee Id="ABC001"> <Name>Prasad 1</Name> <Mobile>9986730630</Mobile> <Address Type="Perminant"> <City>City1</City> <Country>India</Country> </Address> <Address Type="Temporary"> <City>City2</City> <Country>India</Country> </Address> </Employee> Now I want get all Address Type's. I tried like below using XPath and I'm getting exception. var xPathString = @"//Employee/Address/@Type"; doc.XPathSelectElements(xPathString); // doc is XDocument.Load("xml

Prevent XDocument or XElement form Encoding content

坚强是说给别人听的谎言 提交于 2020-05-12 09:01:12
问题 When I call XDocument.Save it is encoding my html <br/> tag, is there a way to prevent this? XDocument xDoc = new XDocument(new XElement("desc","jon skeet <br/> knows, the <br/> answer")); xDoc.Save(Server.MapPath("~/tempUploads/encodeTest.xml")); OUTPUT IS: <?xml version="1.0" encoding="utf-8"?> <desc>jon skeet <br/> knows, the <br/> answer</desc> OUTPUT I WOULD LIKE: <?xml version="1.0" encoding="utf-8"?> <desc>jon skeet <br/> knows, the <br/> answer</desc> 回答1: That's expected behavior: