Understanding Linq To Xml – Descendants return no results

var result = doc.Descendants(“TransactionInformationType”); selects all descendants in the XDocument that have element name “TransactionInformationType” and are in the empty namespace. From you screenshot it seems the element you’re trying to select is in the namespace “https://ssl.ditonlinebetalingssystem.dk/remote/payment” though. You need to specify that explicitly: XNamespace ns = “https://ssl.ditonlinebetalingssystem.dk/remote/payment”; ↑↑ ↑ var result = doc.Descendants(ns + … Read more

Remove empty/blanks elements in collection of XML nodes

A single one-liner could do the job, no need to iterate over all elements. Here it goes: doc.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove(); Tester using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { public class TestRemove { public static void Main() { Console.WriteLine(“—-OLD TREE STARTS—“); XElement doc = XElement.Parse(@”<magento_api> <data_item> <code>400</code> <message>Attribute … Read more

How can I remove empty xmlns attribute from node created by XElement

It’s all about how you handle your namespaces. The code below creates child items with different namespaces: XNamespace defaultNs = “http://www.tempuri.org/default”; XNamespace otherNs = “http://www.tempuri.org/other”; var root = new XElement(defaultNs + “root”); root.Add(new XAttribute(XNamespace.Xmlns + “otherNs”, otherNs)); var parent = new XElement(otherNs + “parent”); root.Add(parent); var child1 = new XElement(otherNs + “child1”); parent.Add(child1); var child2 … Read more

Force XDocument to write to String with UTF-8 encoding

Try this: using System; using System.IO; using System.Text; using System.Xml.Linq; class Test { static void Main() { XDocument doc = XDocument.Load(“test.xml”, LoadOptions.PreserveWhitespace); doc.Declaration = new XDeclaration(“1.0”, “utf-8”, null); StringWriter writer = new Utf8StringWriter(); doc.Save(writer, SaveOptions.None); Console.WriteLine(writer); } private class Utf8StringWriter : StringWriter { public override Encoding Encoding { get { return Encoding.UTF8; } } } … Read more

Finding element in XDocument?

Elements() will only check direct children – which in the first case is the root element, in the second case children of the root element, hence you get a match in the second case. If you just want any matching descendant use Descendants() instead: var query = from c in xmlFile.Descendants(“Band”) select c; Also I … Read more

XDocument.ToString() drops XML Encoding Tag

Either explicitly write out the declaration, or use a StringWriter and call Save(): using System; using System.IO; using System.Text; using System.Xml.Linq; class Test { static void Main() { string xml = @”<?xml version=’1.0′ encoding=’utf-8′?> <Cooperations> <Cooperation /> </Cooperations>”; XDocument doc = XDocument.Parse(xml); StringBuilder builder = new StringBuilder(); using (TextWriter writer = new StringWriter(builder)) { doc.Save(writer); … Read more

How to put attributes via XElement

Add XAttribute in the constructor of the XElement, like new XElement(“Conn”, new XAttribute(“Server”, comboBox1.Text)); You can also add multiple attributes or elements via the constructor new XElement(“Conn”, new XAttribute(“Server”, comboBox1.Text), new XAttribute(“Database”, combobox2.Text)); or you can use the Add-Method of the XElement to add attributes XElement element = new XElement(“Conn”); XAttribute attribute = new XAttribute(“Server”, … Read more

Query XDocument with xmlns attribute (namespace)

Why is it a problem when a xml document contains an xmlns attribute? It’s not, if you understand what it means 🙂 Basically you’ve applied a default namespace URI of “http://schemas.microsoft.com/developer/msbuild/2003” to all elements. So when querying, you need to specify that namespace too. Fortunately, LINQ to XML makes that really simple: XNamespace ns = … Read more

Linq to XML – update/alter the nodes of an XML Document

thank you for your answer. everything works fine. just as completition to my questions the code below shows how to modify a single entry: string xml = @”<data><record id=’1′ info=’sample Info’/><record id=’2′ info=’sample Info’/><record id=’3′ info=’sample Info’/></data>”; StringReader sr = new StringReader(xml); XDocument d = XDocument.Load(sr); d.Descendants(“record”).Where(x => x.Attribute(“id”).Value == “2”).Single().SetAttributeValue(“info”, “new sample info”);