xmldocument

SelectSingleNode returns null even with namespace managing

五迷三道 提交于 2019-12-20 04:32:05
问题 I've come from this question, where my first issue was solved: XML Select a single node where the names are repeating It was a namespace issue first. But now even with the corect NameSpace managing my XPath still returns me null. I've also checked: SelectSingleNode return null - even with namespace SelectSingleNode always returns null? XmlDocument.SelectSingleNode and xmlNamespace issue SelectSingleNode returning null for known good xml node path using XPath Why is SelectSingleNode returning

C# : the close method of Xml.Load(file)

淺唱寂寞╮ 提交于 2019-12-19 08:00:37
问题 I have written some code that loads an XML document using an XmlDocument object so as to count it's nodes. Here is the method: XmlDocument xml = new XmlDocument(); xml.Load(textBox1.Text); XmlNodeList nodes = xml.SelectNodes("//File"); foreach (XmlNode node in nodes) { number_of_childs++; } The problem that I am facing is, when importing a large file, it takes like 700MB of RAM. If I then try to do some operation on the file, or even read from it to display its data in a ListView , the

C# : the close method of Xml.Load(file)

守給你的承諾、 提交于 2019-12-19 08:00:09
问题 I have written some code that loads an XML document using an XmlDocument object so as to count it's nodes. Here is the method: XmlDocument xml = new XmlDocument(); xml.Load(textBox1.Text); XmlNodeList nodes = xml.SelectNodes("//File"); foreach (XmlNode node in nodes) { number_of_childs++; } The problem that I am facing is, when importing a large file, it takes like 700MB of RAM. If I then try to do some operation on the file, or even read from it to display its data in a ListView , the

remove attribute if it exists from xmldocument

强颜欢笑 提交于 2019-12-19 06:37:20
问题 How to remove the attribute from XmlDocument if attribute exists in the document? Please help. I am using RemoveAttribute but how can I check if it exists. root.RemoveAttribute(fieldName); Thanks.. <?xml version="1.0" standalone="yes" ?> <Record1> <Attribute1 Name="DataFieldName" Value="Pages" /> </Record1> I am trying to remove attribute named "DataFieldName". 回答1: Not sure exactly what you're trying to do, so here's two examples. Removing the attribute: var doc = new System.Xml.XmlDocument(

C#: Line information when parsing XML with XmlDocument

情到浓时终转凉″ 提交于 2019-12-19 06:18:44
问题 What are my options for parsing an XML file with XmlDocument and still retain line information for error messages later on? (as an aside, is it possible to do the same thing with XML Deserialisation?) Options seem to include: Extending the DOM and using IXmlLineInfo Using XPathDocument 回答1: The only other option I know of is XDocument.Load() , whose overloads accept LoadOptions.SetLineInfo . This would be consumed in much the same way as an XmlDocument . Example 回答2: (Expanding answer from

XmlDocument.Load Vs XmlDocument.LoadXml

青春壹個敷衍的年華 提交于 2019-12-19 05:06:12
问题 I just came across with a problem using XmlDocument.LoadXml . The application was crashing, giving the following error: "Data at the root level is invalid. Line 1, position 1" After inspecting the XML and finding nothing wrong with it, I googled a bit and found a tip to use XmlDocument.Load instead of XmlDocument.LoadXml . I have tried it and it works perfectly. My question is: What is the difference between the 2 methods and what could have cause one to work and the other to fail? 回答1:

XmlDocument.Load Vs XmlDocument.LoadXml

六眼飞鱼酱① 提交于 2019-12-19 05:06:08
问题 I just came across with a problem using XmlDocument.LoadXml . The application was crashing, giving the following error: "Data at the root level is invalid. Line 1, position 1" After inspecting the XML and finding nothing wrong with it, I googled a bit and found a tip to use XmlDocument.Load instead of XmlDocument.LoadXml . I have tried it and it works perfectly. My question is: What is the difference between the 2 methods and what could have cause one to work and the other to fail? 回答1:

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

爷,独闯天下 提交于 2019-12-19 03:01:15
问题 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

XmlNode.SelectSingleNode syntax to search within a node in C#

谁说胖子不能爱 提交于 2019-12-18 14:54:15
问题 I want to limit my search for a child node to be within the current node I am on. For example, I have the following code: XmlNodeList myNodes = xmlDoc.DocumentElement.SelectNodes("//Books"); foreach (XmlNode myNode in myNodes) { string lastName = ""; XmlNode lastnameNode = myNode.SelectSingleNode("//LastName"); if (lastnameNode != null) { lastName = lastnameNode.InnerText; } } I want the LastName element to be searched from within the current myNode inside of the foreach. What is happening is

Read XML file into XmlDocument

我怕爱的太早我们不能终老 提交于 2019-12-18 10:31:03
问题 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. 回答1: Use XmlDocument.Load() method to load XML from your file. Then use XmlDocument.InnerXml property to get XML string. XmlDocument doc = new XmlDocument(); doc.Load("path to your file"); string xmlcontents = doc.InnerXml; 回答2: If your .NET version is newer than 3.0 you can try using System.Xml.Linq.XDocument instead of XmlDocument . It is easier to process data with