Questions tagged [xmldocument]

The original .NET type that represents an XML document. The more modern version is XDocument.

XmlDocument is the original .NET type that represents an XML document. The more modern version is XDocument.

Use XmlDocument.LoadXml to load an XmlDocument from a string. Use one of the XmlDocument.Load methods to load from a file path or URL, Stream, TextReader, or XmlReader.

Using XmlDocument.Load with a string containing XML will lead to the common "Data at the root level is invalid" exception.

1239 questions
592
votes
7 answers

XDocument or XmlDocument

I am now learning XmlDocument but I've just ran into XDocument and when I try to search the difference or benefits of them I can't find something useful, could you please tell me why you would use one over another ?
Tarik
  • 79,711
  • 83
  • 236
  • 349
256
votes
6 answers

Convert XmlDocument to String

Here is how I'm currently converting XMLDocument to String StringWriter stringWriter = new StringWriter(); XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); xmlDoc.WriteTo(xmlTextWriter); return stringWriter.ToString(); The problem…
akif
  • 12,034
  • 24
  • 73
  • 85
128
votes
7 answers

How to prevent blank xmlns attributes in output from .NET's XmlDocument?

When generating XML from XmlDocument in .NET, a blank xmlns attribute appears the first time an element without an associated namespace is inserted; how can this be prevented? Example: XmlDocument xml = new…
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
119
votes
3 answers

XML Document to String

What's the simplest way to get the String representation of a XML Document (org.w3c.dom.Document)? That is all nodes will be on a single line. As an example, from trge 156 (this is only a tree representation, in my…
bluish
  • 26,356
  • 27
  • 122
  • 180
119
votes
12 answers

What is the simplest way to get indented XML with line breaks from XmlDocument?

When I build XML up from scratch with XmlDocument, the OuterXml property already has everything nicely indented with line breaks. However, if I call LoadXml on some very "compressed" XML (no line breaks or indention) then the output of OuterXml…
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
112
votes
5 answers

Read XML file into XmlDocument

I am very new to C#. I have XML file (text.xml). I want to read that in XmlDocument and store the stream in string variable.
AJP
  • 2,125
  • 3
  • 16
  • 22
84
votes
7 answers

Read XML Attribute using XmlDocument

How can I read an XML attribute using C#'s XmlDocument? I have an XML file which looks somewhat like this:
Alex
  • 75,813
  • 86
  • 255
  • 348
72
votes
5 answers

Deciding on when to use XmlDocument vs XmlReader

I'm optimizing a custom object -> XML serialization utility, and it's all done and working and that's not the issue. It worked by loading a file into an XmlDocument object, then recursively going through all the child nodes. I figured that perhaps…
PhilChuang
  • 2,556
  • 1
  • 23
  • 29
70
votes
13 answers

How would you compare two XML Documents?

As part of the base class for some extensive unit testing, I am writing a helper function which recursively compares the nodes of one XmlDocument object to another in C# (.NET). Some requirements of this: The first document is the source, e.g.…
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
66
votes
4 answers

How to change XML Attribute

How can I change an attribute of an element in an XML file, using C#?
Mike
64
votes
3 answers

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function

I am trying to call SelectNode from XmlDocument class and trouble due to this error: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. My code: public void Add(ref XmlDocument xmlFormat,…
rizwan ansari
  • 657
  • 1
  • 5
  • 5
55
votes
4 answers

SelectSingleNode returns null when tag contains xmlNamespace

I'm loading a string into an XML document that contains the following structure:
Shlomi Komemi
  • 5,445
  • 3
  • 28
  • 41
51
votes
5 answers

How to create a XmlDocument using XmlWriter in .NET?

Many .NET functions use XmlWriter to output/generate xml. Outputting to a file/string/memory is a very operation: XmlWriter xw = XmlWriter.Create(PutYourStreamFileWriterEtcHere); xw.WriteStartElement("root"); ... Sometimes , you need to manipulate…
Boaz
  • 25,331
  • 21
  • 69
  • 77
49
votes
6 answers

Removing nodes from an XmlDocument

The following code should find the appropriate project tag and remove it from the XmlDocument, however when I test it, it says: The node to be removed is not a child of this node. Does anyone know the proper way to do this? public void DeleteProject…
FlySwat
  • 172,459
  • 74
  • 246
  • 311
48
votes
1 answer

How to modify existing XML file with XmlDocument and XmlNode in C#

I already implemented to create the XML file below with XmlTextWriter when application initialization. And know I don't know how to update the childNode id value with XmlDocument & XmlNode. Is there some property to update the id value? I tried…
Nano HE
  • 9,109
  • 31
  • 97
  • 137
1
2 3
82 83