linq-to-xml

XPathSelectElement vs Descendants

烂漫一生 提交于 2019-12-10 12:48:51
问题 I was wondering if there are any performance differences when using simple queries as: var x = document.XPathSelectElement("actors/actor") vs var x = document.Descendants("actors").Descendants("actor") 回答1: Note that this var x = document.Elements("actors").Elements("actor").FirstOrDefault(); is the equivalent of your first statement. There will be a performance difference, because the methods are doing very different things under the hood. However, optimising purely in-memory operations is a

How to get an independent copy of an XDocument?

↘锁芯ラ 提交于 2019-12-10 12:43:57
问题 I'm trying to create a new XDocument as follows: var xmlString = _documentDictionary[documentKey].ToString(SaveOptions.DisableFormatting); XDocument xml = XDocument.Parse(xmlString); I now have xml which I would have though was a stand-alone instance of a document because I extracted the string from the original document and created a new one from that. But when I modify xml and then inspect the _documentDictionary[documentKey] I can see that the original document has been modified also. How

Merging of xml documents

纵饮孤独 提交于 2019-12-10 12:35:38
问题 All of the solutions I have come across regarding merging XML documents do not accomplish what I desire. Let me explain: XML Document 1: <?xml version="1.0" encoding="utf-8" ?> <a> <b title="Original Section"> <b title="Original Child Section"></b> <b title="Original Child Section 2"></b> </b> </a> XML Document 2: <?xml version="1.0" encoding="utf-8" ?> <a> <b title="New Section"> <b title="New Child Section"></b> </b> <b title="Original Section"> <b title="Original Child Section"> <b title=

How do I model a dynamic XML element in a C# serialization class?

霸气de小男生 提交于 2019-12-10 12:09:52
问题 I have an XML document where one of the element nodes can be dynamic, or of any XML structure. I'm having a difficult time modeling the corresponding C# serialization class. For example I have something like this in my C# class: [XmlAnyElement] public XmlNode Value { get; set; } Where XmlNode is System.Xml.XmlNode. A few notes: I want value to be an XML file I'm loading via Linq's XDocument (minus the XML header tag) Though I don't see a way to convert an System.Xml.Linq.XNode to System.Xml

XDocument deleting a node

只愿长相守 提交于 2019-12-10 11:31:20
问题 How do I delete a specific node from a loaded XDocument? My XML document looks like this: <Snippets> <Snippet name="if"> <SnippetCode> if (condition) { } </SnippetCode> </Snippet> <Snippets> <Snippet name="foreach"> <SnippetCode> ... </SnippetCode> </Snippet> .... </Snippets> So say if I wanted to delete just the foreach snippet, how would I do that? I tried doc.Descendants.Remove(), but it didn't work for me (the node didn't get deleted). Edit - on that note, how can I also rename the

Add an element to xml file

人走茶凉 提交于 2019-12-10 11:18:45
问题 I am trying to add and delete elements from a C# .csproj file. The file, in part, appears below. Can someone show me how I can do the following two things? Add an element as shown below (the line that says, "I want to add this") Delete an element. For example, say I wanted to delete the line I have indicated below. <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup>

How do I use LINQ to XML to create a list of objects?

自闭症网瘾萝莉.ら 提交于 2019-12-10 10:55:46
问题 I have some inbound XML that I am trying to read with LINQ to generate a list of objects: <SCResponse> <link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=2&limit=20" rel="next_page" title="Next"/> <link href="http://192.168.6.126:8001/affiliate/account/81/notifications?startRow=1&limit=20" rel="previous_page" title="Previous"/> <RecordLimit>20</RecordLimit> <Notifications> <Notification href="http://192.168.6.126:8001/affiliate/account/81/notifications/24">

How to exclude NULL blocks from XML using LINQ-to-XML?

折月煮酒 提交于 2019-12-10 10:42:38
问题 Consider this XML file. Note the first Tutorial has an Author child element, and the second Tutorial does not: <?xml version="1.0" encoding="utf-8" ?> <Tutorials> <Tutorial> <Author>The Tallest</Author> <Title> WPF Tutorial - Creating A Custom Panel Control </Title> <Date>2/18/2008</Date> </Tutorial> <Tutorial> <Title> 2nd WPF Tutorial - Creating A Custom Panel Control </Title> <Date>2/18/2008</Date> </Tutorial> </Tutorials> How do I use LINQ-to-XML to load the data that is present? The code

Export SQL Server Database Table to XML Using Linq

别等时光非礼了梦想. 提交于 2019-12-10 10:21:27
问题 I know how to do simple Linq To XML. Export a sql server database table data. A simple query like follow will work: xmlDoc = new XElement("TestPoints", from test in myDB.TestPoints select new XElement("TestPoint", new XElement("Id", test.Id), new XElement("Value", test.Value), new XElement("Time", test.Time), new XElement("TestId", test.TestId) ) ); xmlDoc.Save("test.xml"); However, in this case, i need to specify every database column those I need to export. What I need is to export a table

Trying to Filter XML using XPath

只愿长相守 提交于 2019-12-10 10:21:18
问题 I'm trying to filter an XML file using XPath. The XPath that I'm using is definitely filtering to the data that I want, but I'm just not sure how to filter the file overall. Here's the sample XML file: <fields> <field name='F'> <field name='0'><value>F.0 stuff</value></field> <field name='1'><value>F.1 stuff</value></field> <field name='2'><value>F.2 stuff</value></field> </field> <field name='B'> <field name='0'><value>B.0 stuff</value></field> <field name='1'><value>B.1 stuff</value></field