xmlreader

How to use XmlReader class?

安稳与你 提交于 2019-11-30 08:46:22
I want to save and load my xml data using XmlReader. But I don't know how to use this class. Can you give me a sample code for start? Personally I have switched away from XMLReader to System.XML.Linq.XDocument to manage my XML data files. This way I can easily pull data from xml into objects and manage them like any other object in my program. When I am done manipulating them I can just save the changes back out the the xml file at any time. //Load my xml document XDocument myData = XDocument.Load(PhysicalApplicationPath + "/Data.xml"); //Create my new object HelpItem newitem = new HelpItem();

XmlReader - I need to edit an element and produce a new one

℡╲_俬逩灬. 提交于 2019-11-29 13:31:55
I am overriding a method which has an XmlReader being passed in, I need to find a specific element, add an attribute and then either create a new XmlReader or just replace the existing one with the modified content. I am using C#4.0 I have investigated using XElement (Linq) but I can't seem to manipulate an existing element and add an attribute and value. I know that the XmlWriter has WriteAttributeString which would be fantastic but again I am not sure how it all fits together I would like to be able to do something like --- This is pseudo-code! public XmlReader DoSomethingWonderful(XmlReader

From XmlDocument To XmlReader .Net

半城伤御伤魂 提交于 2019-11-29 11:18:49
问题 After an advice from a user that answered to my question I'm tring to convert my XmlDocument code to XmlReader code but I'm having some troubles. This is XML (generated from php-mysql Page) <row> <idLink>64</idLink> <idHost>3</idHost> <url>http://www.google.com</url> </row> <row> <idLink>68</idLink> <idHost>4</idHost> <url>http://www.bing.com</url> </row> ..... until about 10000 rows This is my XmlDocument code: xmlDoc.Load("http://www.myUrl.com/list.php"); if (xmlDoc.DocumentElement != null)

How to best detect encoding in XML file?

≯℡__Kan透↙ 提交于 2019-11-29 10:49:10
To load XML files with arbitrary encoding I have the following code: Encoding encoding; using (var reader = new XmlTextReader(filepath)) { reader.MoveToContent(); encoding = reader.Encoding; } var settings = new XmlReaderSettings { NameTable = new NameTable() }; var xmlns = new XmlNamespaceManager(settings.NameTable); var context = new XmlParserContext(null, xmlns, "", XmlSpace.Default, encoding); using (var reader = XmlReader.Create(filepath, settings, context)) { return XElement.Load(reader); } This works, but it seems a bit inefficient to open the file twice. Is there a better way to detect

Why is my new XmlTextReader(stream) reading in many megabytes into memory rather than streaming properly?

别等时光非礼了梦想. 提交于 2019-11-29 08:42:38
I am getting Out of Memory Exceptions when STREAMING in XML into an XmlReader! Looking in a memory profiler we can see that it is calling StringBuilder.Append over and over resulting in tons of 128KB buffers filling all of memory. That's pretty contrary to "streaming". It shouldn't be loading more than one 4KB buffer. Reading through the .NET source code, it turns out there's a "v1compat" mode that will indeed read way ahead, defeating the purpose of streaming. So, how do you avoid getting it into that stupid mode? It turns out that there's a HUGE difference between calling 'new XmlTextReader

Problem reading files greater than 1GB with XMLReader

落花浮王杯 提交于 2019-11-29 07:47:03
Is there a maximum file size the XMLReader can handle? I'm trying to process an XML feed about 3GB large. There are certainly no PHP errors as the script runs fine and successfully loads to the database after it's been run. The script also runs fine with smaller test feeds - 1GB and below. However, when processing larger feeds the script stops reading the XML File after about 1GB and continues running the rest of the script. Has anybody experienced a similar problem? and if so how did you work around it? Thanks in advance. gazda I had same kind of problem recently and I thought to share my

Reading attribute values with XmlReader

僤鯓⒐⒋嵵緔 提交于 2019-11-29 05:36:48
I have an XML file that I'm trying to read from here , and have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace XML { class Program { static void Main(string[] args) { XmlTextReader textReader = new XmlTextReader("secLendingXML.cfm.xml"); while (textReader.Read()) { switch (textReader.NodeType) { case XmlNodeType.Element: Console.WriteLine(textReader.Name); Console.WriteLine(textReader.Value); break; case XmlNodeType.Text: Console.WriteLine(textReader.Value); break; case XmlNodeType

Replacing the innertext of an Xml node/element

痞子三分冷 提交于 2019-11-28 13:27:22
First of all this is C#. I am creating a internet dashboard for a small group of colleages in the NHS. Below is an example xml file in which I need to change the innertext of. I need to replace a specific element for example "Workshop1." Because we have a few workshops I cannot afford to use a general writer because it will replace all the information on the XML document with this one bit of code below. <?xml version="1.0" ?> <buttons> <workshop1>hello</workshop1> <url1>www.google.co.uk</url1> I am using a switch case to select a specific workshop where you can change the name and add a URL of

XmlReader breaks on UTF-8 BOM

戏子无情 提交于 2019-11-28 12:15:07
I have the following XML Parsing code in my application: public static XElement Parse(string xml, string xsdFilename) { var readerSettings = new XmlReaderSettings { ValidationType = ValidationType.Schema, Schemas = new XmlSchemaSet() }; readerSettings.Schemas.Add(null, xsdFilename); readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; readerSettings.ValidationEventHandler += (o, e) => { throw new

How to create an XML file from a XmlReader?

耗尽温柔 提交于 2019-11-28 12:07:13
How do you write an XML file from an System.Xml.XmlReader? I thought this would be a simple question but whenever I search I seem to be ending up with reading the file to a reader or writing node by node. The XmlReader object conveys xml that was stored in a database and just needs to come out of the database to a file. Is there any easy way to do this? SqlCommand dataCmd = new SqlCommand(sqlText, Conn); System.Xml.XmlReader dataReader = null; dataCmd.CommandTimeout = 60000; Conn.Open(); dataReader = dataCmd.ExecuteXmlReader(); dataReader.Read(); You need to create an XmlWriter and call its