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 an XDocument for elements by name at any depth

Descendants should work absolutely fine. Here’s an example: using System; using System.Xml.Linq; class Test { static void Main() { string xml = @” <root> <child id=’1’/> <child id=’2′> <grandchild id=’3′ /> <grandchild id=’4′ /> </child> </root>”; XDocument doc = XDocument.Parse(xml); foreach (XElement element in doc.Descendants(“grandchild”)) { Console.WriteLine(element); } } } Results: <grandchild id=”3″ /> <grandchild … Read more

How to get a JSON string from URL?

Use the WebClient class in System.Net: var json = new WebClient().DownloadString(“url”); Keep in mind that WebClient is IDisposable, so you would probably add a using statement to this in production code. This would look like: using (WebClient wc = new WebClient()) { var json = wc.DownloadString(“url”); }

What is the difference between Linq to XML Descendants and Elements

Elements finds only those elements that are direct descendents, i.e. immediate children. Descendants finds children at any level, i.e. children, grand-children, etc… Here is an example demonstrating the difference: <?xml version=”1.0″ encoding=”utf-8″ ?> <foo> <bar>Test 1</bar> <baz> <bar>Test 2</bar> </baz> <bar>Test 3</bar> </foo> Code: XDocument doc = XDocument.Load(“input.xml”); XElement root = doc.Root; foreach (XElement e … Read more

Converting XDocument to XmlDocument and vice versa

You can use the built in xDocument.CreateReader() and an XmlNodeReader to convert back and forth. Putting that into an Extension method to make it easier to work with. using System; using System.Xml; using System.Xml.Linq; namespace MyTest { internal class Program { private static void Main(string[] args) { var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(“<Root><Child>Test</Child></Root>”); var xDocument … Read more

LINQ to read XML

Try this. using System.Xml.Linq; void Main() { StringBuilder result = new StringBuilder(); //Load xml XDocument xdoc = XDocument.Load(“data.xml”); //Run query var lv1s = from lv1 in xdoc.Descendants(“level1”) select new { Header = lv1.Attribute(“name”).Value, Children = lv1.Descendants(“level2″) }; //Loop through results foreach (var lv1 in lv1s){ result.AppendLine(lv1.Header); foreach(var lv2 in lv1.Children) result.AppendLine(” ” + lv2.Attribute(“name”).Value); } … Read more

XDocument or XmlDocument

If you’re using .NET version 3.0 or lower, you have to use XmlDocument aka the classic DOM API. Likewise you’ll find there are some other APIs which will expect this. If you get the choice, however, I would thoroughly recommend using XDocument aka LINQ to XML. It’s much simpler to create documents and process them. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)