linq-to-xml

issue removing a XDocument node based on its attribute

你。 提交于 2019-12-24 13:32:22
问题 I'm trying to remove a CreditCard node in a XDocument called doc based on its name attribute but its not working as intended. doc is my XDocument and it looks like this: XDocument doc = new XDocument( new XComment("XML test file"), new XElement("CreditCards", new XElement("CreditCard", new XAttribute("Name", "TestCard1"), new XAttribute("phoneNumber", 121212142121)), new XElement("CreditCard", new XAttribute("Name", "TestCard2"), new XAttribute("phoneNumber", 6541465561)), new XElement(

Getting relative path of file?

我怕爱的太早我们不能终老 提交于 2019-12-24 11:22:05
问题 I have a file called settings.xml located at: c:\solution1\solution1\data\settings.xml Right now, I am doing: XDocument doc = XDocument.Load(@"c:\solution1\solution1\settings.xml"); I can't figure how to do it with a relative path. 回答1: If you mean relative to your executable, you can use string exeLocation = System.Reflection.Assembly.GetExecutingAssembly().CodeBase Note the frequently suggested System.Reflection.Assembly.GetExecutingAssembly().Location will get the path where the assembly

Why isn't my XML file being saved, even though the program sees the updated values next time I start it?

怎甘沉沦 提交于 2019-12-24 11:18:48
问题 I'm reading the contents of an XML file and parsing that into an object model. When I modify the values in the object model, then use the following code to save it back to the xml: XElement optionXml = _panelElement.Elements("options").FirstOrDefault(); optionXml.SetAttributeValue("arming", value.ToString()); _document.Save(_fileName); This works, as far as I can see, because when I close the application and restart it the values that I had saved are reflected in the object model next time I

Datatable to XML using LINQ

雨燕双飞 提交于 2019-12-24 10:58:30
问题 I am writing a method to convert datatable using LINQ. I was able to get straight forward conversion from datatable to XML as below: XDocument doc = new XDocument(new XDeclaration("1.0","UTF-8","yes"), new XElement("pfolios", from p in dt.AsEnumerable() select new XElement("pfolio", new XAttribute("ID", p.ID), new XAttribute("Date", p.Date), new XAttribute("Expired", p.Expiry)))); but I need some help to write a method, that takes datatable with any number of columns as input and write to xml

Linq to Xml, Filter Element from Query (C#)

一世执手 提交于 2019-12-24 10:35:39
问题 I'm simply trying to query an xml document and iterate over the results minus specific elements. Ideally I would like to achieve this in the query rather than removing it from the collection before or during iteration. <body> Stuff I want <element> Stuff I dont want </element> </body> I tried something along these lines but had no luck.... var doc = XDocument.Load("document.xml"); var results = doc.Descendants("body") .Where(x => x.Name != "element") I'm certainly out of my element using XML,

Can I 'flatten' an XDocument with Linq?

浪尽此生 提交于 2019-12-24 10:16:57
问题 I have a heavily nested XML document that I need to load into my db for additional processing. For various reasons beyond the scope of this discussion I need to 'flatten' that structure down, then load it into a DataTables and then I can SQLBulkCopy it into the db where it will get processed. So assume my original XML looks something like this (mine is even more heavily nested, but this is the basic idea): <data> <report id="1234" name="XYZ"> <department id="234" name="Accounting"> <item id=

How to get this attribute using linq to xml?

☆樱花仙子☆ 提交于 2019-12-24 09:03:42
问题 I have the following XML: <Result ID="1,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/"> <ErrorCode>0x00000000</ErrorCode> <ID /> <z:row ows_ID="6" /> </Result> I have been trying to get the ows_ID value using the following methods: XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/"; string newId = (from r in resDoc.Descendants(ns + "row") select (string)r.Attribute("ows_ID")).First(); which returns no records, and: XNamespace ns = "http://schemas.microsoft.com

Using LINQ to XML, how can I join two sets of data based on ordinal position?

守給你的承諾、 提交于 2019-12-24 08:39:35
问题 Using LINQ to XML, how can I join two sets of data based on ordinal position? <document> <set1> <value>A</value> <value>B</value> <value>C</value> </set1> <set2> <value>1</value> <value>2</value> <value>3</value> </set2> </document> Based on the above fragment, I would like to join the two sets together such that "A" and "1" are in the same record, "B" and "2" are in the same record, and "C" and "3" are in the same record. 回答1: This is what the Enumerable.Zip extension does in .NET 4. You'd

How to generate array of objects after LINQ to xml in C#

假装没事ソ 提交于 2019-12-24 08:13:02
问题 I have an object like this: public class ClientCredentials { public string UserName { get; set; } public string Password { get; set; } public string Rights { get; set; } } and an xml looking like this: <?xml version="1.0" encoding="utf-8" ?> <users> <user> <username>playerone</username> <password>654321</password> <rights>true</rights> </user> <user> <username>amoreroma</username> <password>123456789</password> <rights>false</rights> </user> </users> I just want to generate a List of

How to check if XElement has any child nodes?

三世轮回 提交于 2019-12-24 07:58:31
问题 I have the following condition, if (myXElement.FirstNode.NodeType == XmlNodeType.CDATA) This throws an exception if there is no FirstNode in myXElement , so I have to check first if there is any. Note that I need to check for nodes not elements. 回答1: var hasDescendants = myElement.Nodes().Any(); 回答2: Sorry for the VB but wouldn't this work If myXElement.Nodes.Count > 0 AndAlso myXElement.FirstNode.NodeType = Xml.XmlNodeType.CDATA Then End If 来源: https://stackoverflow.com/questions/37811047