linq-to-xml

c# LINQ to XML Query Expression for elemennt exhist ?

<channel> <title>Best Web Gallery - Flash + CSS Gallery</title> <link>http://bestwebgallery.com&lt;/link&gt; <description>Featuring the world best web design</description> <pubDate>09 Dec 2009</pubDate> <generator>http://wordpress.org/?v=2.3.2&lt;/generator&gt; <language>en</language> <...

How to Enumerate variable XML Document in .NET using Linq to XML

Hi, I'm submitting a command to a ssh session and getting an XML response back which is variable depending on the type of query I'm running. I get the following type of XML returned... <CLIOutput> <Results> <ReturnCode>0</ReturnCode> <EventCode>23000</EventCode> <EventSummary>CLI command completed successfully.</EventSumm...

get attributes from xml tree using linq

I'm working with an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <element1 xmlns="http://namespace1/"&gt; <element2> <element3> <element4 attr1="2009-11-09"> <element5 attr2="NAME1"> <element6 attr3="1"> <element7 attr4="1" attr5="5.5" attr6="3.4"/> </element6>...

Merge two Xelements

I'm not quite sure how to ask this, or if this even exists, but I have a need to merge two xelements with one taking precendence over the other, to become just one element. This is may be thought of as similar to the way Outlook 2007 merges two contacts that seem to be the same based of the contact name. The preference here is VB.NET an...

WCF Can't Return XmlElement from XmlDataDocument.DocumentElement

Hi guys, I'm building a WCF web service which returns a composite object that looks similar to the following: [DataContract] public class WebServiceReturn { ... [DataMember] public XmlElement Results { get; set; } ... } When I return a WebServiceReturn object with the following code, e...

how to load string with special characters in to XElement?

Hi, I am trying to load an XML which has special characters such as ö and ® into XElement. But I get an error saying "Invalid character in the given encoding". Where do I set the encoding so that I will be able to load the string? ...

How to get parent and only one child node

Lets say I have this xml: <categories> <category text="Arts"> <category text="Design"/> <category text="Visual Arts"/> </category> <category text="Business"> <category text="Business News"/> <category text="Careers"/> <category text="Investing"/> </category> ...

LINQ to XML: filter a query using XElement.Attributes() collection with both XName and Value

Using LINQ to XML, XElements and XAttributes I'd like to filter a query using an exact match on an XElement's Attributes() IEnumerable collection of XName/Value pairs, against a provided IEnumerable Attributes collection of XName/Value pairs. <container> <!-- logical group of settings --> <group name="group-one"> <!-- setti...

Convert XElement to string

Hi all, I have a simple XElement object XElement xml = new XElement("XML", new XElement ("TOKEN",Session["Token"]), new XElement("ALL_INCLUSIVE", "0"), new XElement("BEACH", "0"), new XElement("DEST_DEP", ddlDest.SelectedVa...

how to get the inner most childs using XLINQ

I have this xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" manifestId="{b91dc6f6-a837-4c5a-bdc8-77a93107af77}" mandatory="No" xmlns="urn:schemas-microsoft-com:PAG:updater-application-block:v2:manifest"> <files base="http:abc/def" has...

Find values from Xml who have same Element Name

I have following xml which same child element (Filed) and I want to get value from each Child element. Sameple XML <root xmlns=""> <books cat="F1" ISBN="01F187597" genre="Programming"> <Field name="Title" val="XML" /> <Field name="Publish Date" val="20010424" /> <Field name="Price" val="43.00" /> </books>...

LINQ to XML Newbie Question

I have an loaded an XML document with the following structure: <?xml version="1.0" encoding="UTF-8" ?> <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"&gt; <sheetData> <row r="1" spans="1:2"> <c r="A1" t="s"> <v>...

extract data from rss feed in c#

Hi, I need to read the woeid from the xml doc below. I need to read and store the data into a string variable so I can query the yahoo weather service. XML returned by query: <query yahoo:count="1" yahoo:created="2009-12-22T08:30:31Z" yahoo:lang="en-US" yahoo:updated="2009-12-22T08:30:31Z" yahoo:uri="http...

Spacing out output with Linq to Xml

How is it possible to force extra spacing between some nodes using Linq to Xml? I am looking to output the following: <root> <group> <leaf /> </group> <group> <leaf /> </group> </root> By adding Empty XText, it only destroys the formatting. var root = new XElement("root", new XText(""), ...

LINQ to XML and WIX issue

I have a WIX XML document that contains 2,000+ file tags. I am trying to make a program using LINQ to XML that can update an attribute of each file tag. My code is as follows for loading the current attributes into a dictionary. XElement root = XElement.Load(filePath); XNamespace wix = @"http://schemas.microsoft.com/wix/2006/wi"; ...

explicit cast on XAttribute value

I recently wrote a piece of code that looked a bit like this: IEnumerable<DTO.Employee> xEmployee = from e in xDoc.Descendants("Employee") where int.Parse(e.Attribute("Id").Value) == emp.Id select new DTO.Employee { Id = (int)e.Attribute("Id"), LastName = (string)e.Element("LastName"), FirstName =...

can I use Linq to Xml to alter an XSD?

I've got a node in an XSD that I'd like to modify. I'd like to change the "name" value in this node: <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> However when I try to find that node via this code, I get either no nodes or an error, depending on what I try. xsdDoc.Descendants("element").Where...

Optimize Code by using yield or changing algorithm

The code below works but I want to optimize the code using yield or by changing algorithm. public IEnumerable<Book> GetAuthorWithBookName(string keyword) { var bookAndAuthorList = new List<Book>(); List<Author> AuthorNameList = getAuthorName(keyword); foreach (var author in AuthorNameList) { XmlNode booksNames = ...

Problem seeing XML declaration on System.Console

Hi, I have been experimenting with LINQ to XML and have run across a very basic problem. For some reason, I am not seeing a XML declaration when dumping the tree to System.Console. using System; using System.Xml.Linq; ... public static void Main(string[] args) { // Build tree. XDocument xd = new XDocument(new XDeclaration("...

Linq to XML - Adding an Element

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? 1) Add an element as shown below (the line that says, "I want to add this") 2) Delete an element. For example, say I wanted to delete the line I have indicated below. <?xml version...