xmldocument

Get a specific number of results from an XmlDocument XPath query

余生长醉 提交于 2019-12-01 03:11:49
问题 I'm querying a Twitter RSS feed and supplying the results into a Repeater for display. I'd like to only get the first 5 results of the XPath query. Is there a way to do that in the XPath syntax or do I have to loop over the resulting XmlNodeList to pull out the first 5? XmlDocument doc = new XmlDocument(); XmlTextReader reader = new XmlTextReader(rssPath); doc.Load(reader); XmlNodeList items = doc.SelectNodes("/rss/channel/item"); rptTwitter.ItemDataBound += new RepeaterItemEventHandler

Using XPath to parse an XML document

主宰稳场 提交于 2019-12-01 03:06:49
Lets say I have the following xml (a quick example) <rows> <row> <name>one</name> </row> <row> <name>two</name> </row> </rows> I am trying to parse this by using XmlDocument and XPath (ultimately so I can make a list of rows). For example... XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); foreach(XmlNode row in doc.SelectNodes("//row")) { string rowName = row.SelectSingleNode("//name").InnerText; } Why, within my foreach loop, is rowName always "one"? I am expecting it to be "one" on the first iteration and "two" on the second. It seems that //name gets the first instance in the

Comparing XmlDocument for equality (content wise)

倖福魔咒の 提交于 2019-12-01 02:40:54
If I want to compare the contents of a XMlDocument, is it just like this? XmlDocument doc1 = GetDoc1(); XmlDocument doc2 = GetDoc2(); if(doc1 == doc2) { } I am not checking if they are both the same object reference, but if the CONTENTS of the xml are the same. LBushkin No. XmlDocument does not override the behavior of the Equals() method so, it is in fact just performing reference equality - which will fail in your example, unless the documents are actually the same object instance. If you want to compare the contents (attributes, elements, commments, PIs, etc) of a document you will have to

Is XMLDocument.Save an atomic operation?

人盡茶涼 提交于 2019-12-01 02:24:03
问题 Is there anyway another process monitoring for files created using XMLDocument.Save() could encounter a partial file? Does it make any difference if Save() is overwriting an existing file? 回答1: If you save like this you shouldn't have any problems. using (var file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) { xmlDoc.Save(file); } 回答2: I don't think that there is any guarantee of atomicity. You should not depend on it. 回答3: Writing files is, in general,

How to remove all child nodes of an XmlElement, but keep all attributes?

社会主义新天地 提交于 2019-12-01 02:22:24
How to remove all child nodes of an XmlElement , but keep all attributes? Note, that XmlElement.RemoveAll also removes all attributes. What is a clean, elegant and well-performing way to remove all child nodes? In other words, what is best-practice here? For a truly efficient solution: e.IsEmpty = true; is your fastest and simplest option. It does exactly what you requested: all inner text and nested elements are discarded, while attributes are retained. Would this solution not be simpler? while(e.FirstChild != null) e.RemoveChild(e.FirstChild); Option 1 Use elem.InnerXml = ""; The full

How to remove all child nodes of an XmlElement, but keep all attributes?

ぃ、小莉子 提交于 2019-11-30 22:41:44
问题 How to remove all child nodes of an XmlElement , but keep all attributes? Note, that XmlElement.RemoveAll also removes all attributes. What is a clean, elegant and well-performing way to remove all child nodes? In other words, what is best-practice here? 回答1: For a truly efficient solution: e.IsEmpty = true; is your fastest and simplest option. It does exactly what you requested: all inner text and nested elements are discarded, while attributes are retained. 回答2: Would this solution not be

Comparing XmlDocument for equality (content wise)

北战南征 提交于 2019-11-30 22:29:48
问题 If I want to compare the contents of a XMlDocument, is it just like this? XmlDocument doc1 = GetDoc1(); XmlDocument doc2 = GetDoc2(); if(doc1 == doc2) { } I am not checking if they are both the same object reference, but if the CONTENTS of the xml are the same. 回答1: No. XmlDocument does not override the behavior of the Equals() method so, it is in fact just performing reference equality - which will fail in your example, unless the documents are actually the same object instance. If you want

What is the easiest way to convert this XML document to my object?

天大地大妈咪最大 提交于 2019-11-30 20:43:14
I have an XMLDocument that i need to read in and convert into a set of objects. I have the following objects public class Location { public string Name; public List<Building> Buildings; } public class Building { public string Name; public List<Room> Rooms; } and i have the following XML file: <?xml version="1.0" encoding="utf-8" ?> <info> <locations> <location name="New York"> <Building name="Building1"> <Rooms> <Room name="Room1"> <Capacity>18</Capacity> </Room> <Room name="Room2"> <Capacity>6</Capacity> </Room> </Rooms> </Building> <Building name="Building2"> <Rooms> <Room name="RoomA">

Parse malformed XML

拟墨画扇 提交于 2019-11-30 20:23:02
I'm trying to load a piece of (possibly) malformed HTML into an XMLDocument object, but it fails with XMLExceptions... since there are extra opening/closing tags, and malformed XML tags such as <img > instead of <img /> How do I get the XML to parse with all the errors in the data? Is there any XML validator that I can apply before parsing, to correct these errors? Or would handling the exception parse whatever can be parsed? The HTML Agility Pack will parse html, rather than xhtml, and is quite forgiving. The object model will be familiar if you've used XmlDocument . annakata You might want

How to insert a xml node as first child in another xml document in java?

前提是你 提交于 2019-11-30 18:15:56
问题 for eg. root= <root> <param value="abc"> <param value="bc"> </root> NodeToInsert could be <insert><parameterDesc>afds</parameterDesc></insert> The output should be: <root> <insert><parameterDesc>afds</parameterDesc></insert> <param value="abc"> <param value="bc"> </root> 回答1: I'll be really irritated if it turns out I just did your homework for you. package com.akonizo.examples; import java.io.ByteArrayInputStream; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import