xmlwriter

XmlReader/XmlWriter 类

一曲冷凌霜 提交于 2020-02-27 07:02:06
  XmlReader用于读取Xml文件,XmlWriter用于将数据写到Xml文件。其实,在印象当中,XML很多的操作类都支持直接Save、Read也支持接受XmlReader与XmlWriter类的示例作为参数,但是为什么还要有这个两类来专门用于读写XML文件呢?因为它们有强大的自定义格式功能; 一、XmlReader的使用   XmlReader类专门用于读取Xml文件,最大的特点在于支持 Settings。 属性 说明 AttributeCount 当在派生类中被重写时,获取当前节点上的属性数 BaseURI 当在派生类中被重写时,获取当前节点的基 URI CanReadBinaryContent 获取一个值,该值指示 XmlReader 是否实现二进制内容读取方法 Depth 获取 XML 文档中当前节点的深度 EOF 获取一个值,该值指示此读取器是否定位在流的结尾 HasAttributes 获取一个值,该值指示当前节点是否有任何属性 HasValue 获取一个值,该值指示当前节点是否可以具有 Value IsDefault 获取一个值,该值指示当前节点是否是从 DTD 或架构中定义的默认值生成的特性 IsEmptyElement 获取一个值,该值指示当前节点是否为空元素(例如 <MyElement/>) Item 获取具有指定索引的属性的值,支持整形,字符串

Can anyone tell me why my XML writer is not writing attributes?

[亡魂溺海] 提交于 2020-01-06 13:57:14
问题 I am writing a parsing tool to help me clean up a large VC++ project before I make .net bindings for it. I am using an XML writer to read an xml file and write out each element to a new file. If an element with a certain name is found, then it executes some code and writes an output value into the elements value. So far it is almost working, except for one thing: It is not copying the attributes. Can anyone tell me why this is happening? Here is a sample of what it is supposed to copy/modify

what's the fastest way to write XML

二次信任 提交于 2020-01-02 02:32:12
问题 I need create XML files frequently and I choose XmlWrite to do the job, I found it spent much time on things like WriteAttributeString ( I need write lots of attributes in some cases), my question is are there some better way to create xml files? Thanks in advance. 回答1: Fastest way that I know is two write the document structure as a plain string and parse it into an XDocument object: string str = @"<?xml version=""1.0""?> <!-- comment at the root level --> <Root> <Child>Content</Child> <

Possible to write XML to memory with XmlWriter?

旧城冷巷雨未停 提交于 2019-12-30 03:44:07
问题 I am creating an ASHX that returns XML however it expects a path when I do XmlWriter writer = XmlWriter.Create(returnXML, settings) But returnXML is just an empty string right now (guess that won't work), however I need to write the XML to something that I can then send as the response text. I tried XmlDocument but it gave me an error expecting a string. What am I missing here? 回答1: If you really want to write into memory, pass in a StringWriter or a StringBuilder like this: using System;

Adding multiple namespace declarations in XmlWriter

旧街凉风 提交于 2019-12-29 04:09:32
问题 I am trying to write out the following element using XmlWriter <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"> I've got the very first declaration done using writer.WriteStartElement("kml", "http://www.opengis.net/kml/2.2"); How can I add the remaining 3 declarations to the same element? 回答1: writer.WriteAttributeString("xmlns","gx", null, "http://www.google.com/kml

How can I remove the BOM from XmlTextWriter using C#?

江枫思渺然 提交于 2019-12-28 06:00:35
问题 How do remove the BOM from an XML file that is being created? I have tried using the new UTF8Encoding(false) method, but it doesn't work. Here is the code I have: XmlDocument xmlDoc = new XmlDocument(); XmlTextWriter xmlWriter = new XmlTextWriter(filename, new UTF8Encoding(false)); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); xmlWriter.WriteStartElement("items"); xmlWriter.Close(); xmlDoc.Load(filename); XmlNode

Custom xmlWriter to skip a certain element?

和自甴很熟 提交于 2019-12-28 04:36:06
问题 I am implementing a custom xmlwriter and I want to omit any elements with a particular name, like <ABC> . How do I actually do that? I get a sense that I might need to overwrite the WriteStartElement method like this: public override void WriteStartElement(string prefix, string localName, string ns) { if (localName.Equals("ABC")) { return; } base.WriteStartElement(prefix, localName, ns); } Do I also need to overwrite WriteEndElement method? How do I tell WriteEndElement method to skip writing

c# xml序列化和反序列化。也就是xml的解析和反解析。

断了今生、忘了曾经 提交于 2019-12-26 02:26:17
用习惯了newTownSoft.json 的json反序列化。碰到xml是真的不习惯。 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名。然后直接newTownSoft反序列化,一下就得到一个实体类了。今天调某个接口,碰到xml。 记录如下。 xml <response> <functionID>setItemsPics</functionID> <time>2017-09-07 15:51:04</time> <ItemsIDList>   <itemIDInfo>     <operCode>0</operCode>     <operation>操作成功</operation>   </itemIDInfo> <itemIDInfo>   <itemID>1125507057</itemID>     <operCode>0</operCode>     <operation>操作成功</operation>   </itemIDInfo> </ItemsIDList> </response> 实体类如下:先引用 using System.Xml.Serialization; using System; using System.Collections.Generic; using System.Linq; using

How to turn ON/OFF an XmlWriter Indent property?

吃可爱长大的小学妹 提交于 2019-12-25 05:15:47
问题 I would like to turn ON/OFF the Indent property of a XmlWriter when I need it, to be able to write with this formatting: <?xml version="1.0" encoding="Windows-1252"?> <Songs> <Song><Name>My Song 1.mp3</Name><Year>2007</Year></Song> <Song><Name>My Song 2.mp3</Name><Year>2009</Year></Song> <Song><Name>My Song 3.mp3</Name><Year>2008</Year></Song> </Songs> The problem is I don't know how to modify the property, I've read in other SO question that is not possibly to set this property more than

Windows 8 store app XmlWriter does not write anything to file

感情迁移 提交于 2019-12-25 03:56:06
问题 I am not very well versed with .NET's XML libraries and trying to wrap my head around IXmlSerializable interface. I am developing a windows store app (W8) and wish to use the IXmlSerializable interface, however am running into issues. XmlTextWriter is not available in windows store apps so I am using XmlWriter's factory method but my code does not output anything to the XML file (its blank). If I use XmlSerializer, the XML file is output as expected, but wish to implement IXmlSerializable