xmldocument

Reading XML with an “&” into C# XMLDocument Object

别等时光非礼了梦想. 提交于 2019-11-26 18:18:38
问题 I have inherited a poorly written web application that seems to have errors when it tries to read in an xml document stored in the database that has an "&" in it. For example there will be a tag with the contents: "Prepaid & Charge". Is there some secret simple thing to do to have it not get an error parsing that character, or am I missing something obvious? EDIT: Are there any other characters that will cause this same type of parser error for not being well formed? 回答1: The problem is that

How to Select XML Nodes with XML Namespaces from an XmlDocument?

依然范特西╮ 提交于 2019-11-26 17:53:12
my code attempts to grab data from the RSS feed of a website. It grabs the nodes fine, but when attempting to grab the data from a node with a colon, it crashes and gives the error "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." The code is shown below: WebRequest request = WebRequest.Create("http://buypoe.com/external.php?type=RSS2&lastpost=true"); WebResponse response = request.GetResponse(); StringBuilder sb = new StringBuilder(""); System.IO.StreamReader rssStream = new System.IO.StreamReader(response.GetResponseStream(), System.Text

Why “Data at the root level is invalid. Line 1, position 1.” for XML Document?

血红的双手。 提交于 2019-11-26 14:20:14
问题 I am using a third-party DLL which transmits an XML document over the internet. Why would the DLL be throwing the following exception? Data at the root level is invalid. Line 1, position 1. (see below for full exception text.) Here are the first few lines of the XML Document: <?xml version="1.0" encoding="utf-8"?> <REQUEST> <HEADER> <REQUESTID>8a5f6d56-d56d-4b7b-b7bf-afcf89cd970d</REQUESTID> <MESSAGETYPE>101</MESSAGETYPE> <MESSAGEVERSION>3.0.2</MESSAGEVERSION> Exception: System

Creating an XML document using namespaces in Java

…衆ロ難τιáo~ 提交于 2019-11-26 13:56:02
问题 I am looking for example Java code that can construct an XML document that uses namespaces. I cannot seem to find anything using my normal favourite tool so was hoping someone may be able to help me out. 回答1: I am not sure, what you trying to do, but I use jdom for most of my xml-issues and it supports namespaces (of course). The code: Document doc = new Document(); Namespace sNS = Namespace.getNamespace("someNS", "someNamespace"); Element element = new Element("SomeElement", sNS); element

How to change XML Attribute

筅森魡賤 提交于 2019-11-26 13:07:49
How can I change an attribute of an element in an XML file, using C#? El Padrino Mike; Everytime I need to modify an XML document I work it this way: //Here is the variable with which you assign a new value to the attribute string newValue = string.Empty; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFile); XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element"); node.Attributes[0].Value = newValue; xmlDoc.Save(xmlFile); //xmlFile is the path of your file to be modified I hope you find it useful alexmac Using LINQ to xml if you are using framework 3.5: using System.Xml.Linq;

Deciding on when to use XmlDocument vs XmlReader

徘徊边缘 提交于 2019-11-26 12:19:51
问题 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 using XmlReader instead of having XmlDocument loading/parsing the entire thing would be faster, so I implemented that version as well. The algorithms are exactly the same, I use a wrapper class to abstract the functionality of dealing with an

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

廉价感情. 提交于 2019-11-26 12:05:57
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 stays that way. So ... What is the simplest way to get beautified XML output from an instance of XmlDocument ? Neil C. Obremski Based on the other answers, I looked into XmlTextWriter and came up with the following helper method: static public string Beautify(this XmlDocument doc) { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings =

How would you compare two XML Documents?

拥有回忆 提交于 2019-11-26 11:16:10
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. what I want the XML document to look like. Thus the second is the one I want to find differences in and it must not contain extra nodes not in the first document. Must throw an exception when too many significant differences are found, and it should be easily understood by a human glancing at the description. Child element order is important, attributes

Convert XmlDocument to String

青春壹個敷衍的年華 提交于 2019-11-26 10:09:14
问题 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 with this method is that if I have \" ((quotes) which I have in attributes) it escapes them. For Instance: <Campaign name=\"ABC\"> </Campaign> Above is the expected XML. But it returns <Campaign name=\\\"ABC\\\"> </Campaign> I can do String.Replace \"

Data at the root level is invalid. Line 1, position 1 -why do I get this error while loading an xml file?

拈花ヽ惹草 提交于 2019-11-26 09:44:51
问题 Data at the root level is invalid. Line 1, position 1 -why I get this error while load xml file this my code: XmlDocument xmlDoc=new XmlDocument(); xmlDoc.LoadXml(\"file.xml\"); 回答1: The LoadXml method is for loading an XML string directly. You want to use the Load method instead. 来源: https://stackoverflow.com/questions/7544475/data-at-the-root-level-is-invalid-line-1-position-1-why-do-i-get-this-error-w