xdocument

Change order of XML using XDocument

I want to change the order of XML using XDocument <root> <one>1</one> <two>2</two> </root> I want to change the order so that 2 appears before 1. Is this capability baked in or do I have to do it myself. For example, remove then AddBeforeSelf()? Thanks ...

ViewModel on top of XDocument

I am working on a WPF application which has a treeview that represents an XML. I load the XML on to the XDocument, then bind the TreeView to this object. Now using the MVVM pattern, I want to provide a ViewModel on top of XDocument. What are some of the things that I should implement in the ViewModel class. I am thinking of, Routed...

How to correctly open a FileStream for usage with an XDocument

I want to append some nodes to an xml document using Linq2XML. The file in question is being used by other processes and they should be able to read the file while I update it. So I came up with this solution, which obviously isn't the correct way (The method doc.Save() fails and says that another process is using the file): using (File...

What's the most efficient way to locate and set element values in an XDocument?

Given the following XML 'template': <Request module="CRM" call="list_service_features" id="{ID}"> <block name="auth"> <a name="username" format="text">{USERNAME}</a> <a name="password" format="password">{PASSWORD}</a> <a name="client-id" format="counting">{CLIENT-ID}</a> </block> <a name="service-id" format="countin...

How do I use XComment when reading in an XML document?

I'm using the following line to read in an XML document that may or may not have some comments bracketed by "<!-- -->" near the top of my XML file: XDocument xe1 = XDocument.Load(filepath) How do I read in the comments and store as a string? I'm doing this in MS Visual Studio C#. I know there's something called "XComment", but I c...

How does one test a file to see if it's a valid XML file before loading it with XDocument.Load()?

I'm loading an XML document in my C# application with the following: XDocument xd1 = new XDocument(); xd1 = XDocument.Load(myfile); but before that, I do test to make sure the file exists with: File.Exists(myfile); But... is there an (easy) way to test the file before the XDocument.Load() to make sure it's a valid XML file? In oth...

Add XML Namespace attribute to 3rd party xml?

Hi there I'm using VB 2008 and I'm trying to add a xmlns="mynamespace" attribute to an XDocument's root element. The XML document is created by a 3rd party, and I have loaded it into a VB XDocument object. As it comes, it has no namespaces. I have been working on a local copy and I added in a namespace in a text editor, so that I can u...

xdocument question

I have two documents both are similar but I need to find an elegant and efficient way to compare the two files and return the values in Doc #1 that don't exist in Doc #2. XML Doc #1 <ids> <id>1</id> <id>2</id> <id>5</id> <id>6</id> <id>7</id> <id>8</id> <id>9</id> </i...

Query an XDocument for elements by name at any depth

I have an XDocument object. I want to query for elements with a particular name at any depth using LINQ. When I use Descendants("element_name"), I only get elements that are direct children of the current level. What I'm looking for is the equivalent of "//element_name" in XPath...should I just use XPath, or is there a way to do it us...

Xml Element with namespace

We are starting to use nhibernate and have setup a Session Manager to create a new SessionFactory. I need to modify some information the 1st time the app starts. I open the config file (not app.config) using an XDocument. <settings> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <reflection-optimizer use="fa...

XDocument.Descendants() not returning any elements

I'm trying to bind a Silverlight DataGrid to the results of a WCF service call. I was not seeing the data displayed in the grid, so when I ran through the debugger, I notice that XDocument.Descendants() was not returning any elements even when I was passing in a valid element name. Here is the XML that is passed back from the service: <...

Can I use XDocument.Save and exclude the XML Declaration?

I'm loading a XML file into a XDocument, doing stuff to it, and calling Save(fileName) to rewrite the XML file. The original file has no XML declaration, is there anyway to avoid XDocument adding a XML declaration? ...

Populate XDocument from String

I'm working on a little something and I am trying to figure out whether I can load an XDocument from a string. XDocument.Load() seems to take the string passed to it as a path to a physical XML file. I want to try and bypass the step of first having to create the physical XML file and jump straight to populating the XDocument. Any idea...

Convert XDocument to Stream

How do I convert the xml in an XDocument to a MemoryStream? ...

How to Get XML Node from XDOcument

Hi, How to Get an XML Element from XDocument using LINQ ? Suppose I have an XDocument Named XMLDoc which is shown below: <Contacts> <Node> <ID>123</ID> <Name>ABC</Name> </Node> <Node> <ID>124</ID> <Name>DEF</Name> </Node> </Contacts> XElement Contacts = from xml2...

Does XDocument.Save(string filename) resave the whole file or just changes?

Basically if I do Xdoc.Load(filename), do some changes then do Xdoc.Save(filename) does it only save things that changed such as inserted or removed elements, etc, or does it resave everything? Depending on the answer I'm thinking of determining whether my app is going to save per-change or save on explicit save and on exit. Also consi...

Is there an easy way to compare if 2 XDocuments are equal ignoring element/attribute order?

Unit testing my serialization code I found one failed because I had attributes listed in a different order (I'm just comparing the XDocument.ToString() values) and while I could fix that, it really doesn't matter to me in what order the elements or attributes appear as long as they're all there with the right name at the right level of h...

XDocument can't load xml with version 1.1 in C# LINQ?

XDocument.Load throws exception, when using XML file name of xml with version 1.1 instead of 1.0 Any clean solutions to resolve the error (No regex) and load the document? ...

Validating Xml with Xsd

I'm running into real difficulties validating xml with xsd. I should prefix all of this and state up front, I'm new to xsd and validation, so I'm not sure if it's a code issue or an xml issue. I've been to xml api hell and back with the bajillion different options and think that I've found what would be the ideal strategy for validating ...

How to print <?xml version="1.0"?> using XDocument

Is there any way to have an XDocument print the xml version when using the ToString method? Have it output something like this: <?xml version="1.0"?> <!DOCTYPE ELMResponse [ ]> <Response> <Error> ... I have the following: var xdoc = new XDocument(new XDocumentType("Response", null, null, "\n"), ... which will print this which is fine...