linq-to-xml

LINQ to XML querying for sibling elements

南楼画角 提交于 2019-12-10 10:16:35
问题 Sorry if this question is very basic, but I haven't worked with XML very much, and this is my first time working with LINQ to XML... I have an XML sitemap that is structured like a directory tree: <Site> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="FolderName"> <Security> <Role>Admin</role> </Security> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="subFoler"> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>

how to remove Xelement without its children node using LINQ?

喜欢而已 提交于 2019-12-10 10:15:13
问题 Here is my XML , <A> <B id="ABC"> <C name="A" /> <C name="B" /> </B> <X> <B id="ZYZ"> <C name="A" /> <C name="B" /> </B> </X> </A> I'm using following code to remove <X> node without deleting its descents/childrens, XDocument doc = XDocument.Load("D:\\parsedXml.xml"); doc.Descendants("A").Descendants("X").Remove(); But is removing entire <X> block. Expected output : <A> <B id="ABC"> <C name="A" /> <C name="B" /> </B> <B id="ZYZ"> <C name="A" /> <C name="B" /> </B> </A> 回答1: var x = doc.Root

Extracting data from CDATA using LINQ to XML

丶灬走出姿态 提交于 2019-12-10 09:38:46
问题 I have the following xml file and I am trying to use linq to xml to get the Elements which are residing inside the CDATA section. Any suggestions please. <?xml version = "1.0" encoding = "UTF-8"?> <result final = "true" transaction-id="84WO" xmlns="http://cp.com/rules/client"> <client id = "CustName'> <quoteback> </client> <report format = "CP XML"> <![CDATA[<?xml version="1.0" encoding = "UTF-8" standalone = "yes"?> <personal_auto xmlns = "http://cp.com/rules/client"> <admin> </admin>

Convert XML to VB.NET Dictionary

你说的曾经没有我的故事 提交于 2019-12-10 08:32:02
问题 I'm trying to put sub-child values from XML into a dictionary collection using LINQ. I've done this with lists and custom collections which follow the same structure as the XML but am unable to search for specific values. If I know parentName, childName, and subChildName I want to be able to find subChildProperty1.value and subChildProperty2.value without iterating through the entire collection and each of the subsequent sub-collections, as I have to do with lists. This may not be the best

Linq to XML nested query

纵饮孤独 提交于 2019-12-10 05:45:06
问题 I'm having an issue getting a LINQ query to work. I have this XML: <devices> <device id ="2142" name="data-switch-01"> <interface id ="2148" description ="Po1"/> </device> <device id ="2302" name="data-switch-02"> <interface id ="2354" description ="Po1"/> <interface id ="2348" description ="Gi0/44" /> </device> </devices> And this code: var devices = from device in myXML.Descendants("device") select new { ID = device.Attribute("id").Value, Name = device.Attribute("name").Value, }; foreach

LINQ to XML via C#

拜拜、爱过 提交于 2019-12-10 04:44:49
问题 I'm new to LINQ. I understand it's purpose. But I can't quite figure it out. I have an XML set that looks like the following: <Results> <Result> <ID>1</ID> <Name>John Smith</Name> <EmailAddress>john@example.com</EmailAddress> </Result> <Result> <ID>2</ID> <Name>Bill Young</Name> <EmailAddress>bill@example.com</EmailAddress> </Result> </Results> I have loaded this XML into an XDocument as such: string xmlText = GetXML(); XDocument xml = XDocument.Parse(xmlText); Now, I'm trying to get the

Is it just me? I find LINQ to XML to be sort of cumbersome, compared to XPath

僤鯓⒐⒋嵵緔 提交于 2019-12-10 04:06:27
问题 I am a C# programmer, so I don't get to take advantage of the cool XML syntax in VB. Dim itemList1 = From item In rss.<rss>.<channel>.<item> _ Where item.<description>.Value.Contains("LINQ") Or _ item.<title>.Value.Contains("LINQ") Using C#, I find XPath to be easier to think about, easier to code, easier to understand, than performing a multi-nested select using LINQ to XML. Look at this syntax, it looks like Greek swearing: var waypoints = from waypoint in gpxDoc.Descendants(gpx + "wpt")

How to delete specific nodes from an XElement?

拥有回忆 提交于 2019-12-10 02:36:49
问题 I have created a XElement with node which has XML as below. I want to remove all the " Rule " nodes if they contain " conditions " node. I create a for loop as below but it does not delete my nodes foreach (XElement xx in xRelation.Elements()) { if (xx.Element("Conditions") != null) { xx.Remove(); } } Sample: <Rules effectNode="2" attribute="ability" iteration="1"> <Rule cause="Cause1" effect="I"> <Conditions> <Condition node="1" type="Internal" /> </Conditions> </Rule> <Rule cause="cause2"

How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes

落爺英雄遲暮 提交于 2019-12-10 02:09:34
问题 I have an xml document which looks like this: <Applications> <myApp> <add key="ErrorDestinationEventLog" value="EventLog" /> <add key="version" value="5.0.0.0" /> <add key="DebugMode_RUN" value="true" /> </myApp> </Applications> All the elements have same element name but different attributes. How do I remove one particular element and it's attributes from this xml using XDocument in C#? xd.Element("Applications").Element("myApp").Element(xe.Name).RemoveAll(); The above command is not working

Linq-to-XML XElement.Remove() leaves unwanted whitespace

久未见 提交于 2019-12-10 01:57:02
问题 I have an XDocument that I create from a byte array (received over tcp/ip). I then search for specific xml nodes (XElements) and after retrieving the value 'pop' it off of the Xdocument by calling XElement.Remove(). After all of my parsing is complete, I want to be able to log the xml that I did not parse (the remaining xml in the XDocument). The problem is that there is extra whitespace that remains when XElement.Remove() is called. I want to know the best way to remove this extra whitespace