C#XML Reader简化版有帮助吗?

编程入门 行业动态 更新时间:2024-10-25 00:26:31
本文介绍了C#XML Reader简化版有帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是CS的新学生.现在是夏天,我正在尝试自学,以便提高我的编程技能! 我想了解更多有关XML读取器和写入器的信息.有很多网站,但我不确定该遵循哪个网站.我创建了以下方法,是否为了简化可读性而对其进行了简化?看起来很丑

Hi I am a new CS student. Right now it is summer and I am trying to self-learn so I can keep my programming skills up! I want to know a little more about the XML reader and writer. There are many websites but I am not sure which to follow. I have created the following methods, is there a simplified version of it for readability? It kind of looks ugly

public void AddArchiveInfo(XmlTextReader reader) { XmlNodeType type; while (reader.Read()) { type = reader.NodeType; if (type == XmlNodeType.Element) { if (reader.Name == "ArchiveItem") { ifArchiveItem(reader); } if (reader.Name == "fileExtensions") { ifFileExtension(reader); } } } }

public void ifArchiveItem(XmlTextReader reader) { reader.Read(); ArchiveItem item = new ArchiveItem(); if (reader.Name == "PcName") { reader.Read(); item.pcName = reader.Value; reader.Read(); reader.Read(); } if (reader.Name == "ArchivePrefix") { reader.Read(); item.archivePrefix = reader.Value; reader.Read(); reader.Read(); } if (reader.Name == "FilePath") { reader.Read(); item.filePath = reader.Value; reader.Read(); reader.Read(); } if (reader.Name == "ArchivePath") { reader.Read(); item.archivePath = reader.Value; reader.Read(); reader.Read(); } if (reader.Name == "RetentionDays") { reader.Read(); item.retentionDays = Convert.ToInt32(reader.Value); } this.archiveList.Add(item); }

推荐答案

一种非常好的处理XML的方法是LINQ to XML A very nice way to process XML is LINQ to XML

此代码不好(很好,是不可接受的),因为您使用立即字符串常量对元数据进行了硬编码.这是不支持的.如果您使用数据协定,则不必手动使用XML,因为它不包含硬编码,并且是最不介入的.您只需根据合同持久化数据对象的任何对象图即可. 参见: msdn.microsoft/en-us/library/ms733127.aspx [ ^ ]. 另请参阅我过去的答案,其中我主张采用这种可靠的方法: 如何在我的表单应用程序? [ ^ ], 创建属性文件... [ ^ ]. 关于XmlReader,最好的阅读方法是: msdn.microsoft. com/en-us/library/system.xml.xmlreader.aspx [ ^ ].我不需要所有那些许多网站".在浪费时间之前,尝试从MSDN开始.
—SA
This code is bad (well, unacceptable) because you hard-code meta-data using immediate string constants. This is unsupportable. You don''t have to work with XML manually if you use Data Contract, which excluded hard-coding and is the most non-intrusive. You just persist any object graph of data objects according to the contract. See: msdn.microsoft/en-us/library/ms733127.aspx[^]. See also my past answers where I advocate this robust approach: How can I utilize XML File streamwriter and reader in my form application?[^], Creating a property files...[^]. As to XmlReader, the best reading is this: msdn.microsoft/en-us/library/system.xml.xmlreader.aspx[^]. I would not need all those "many Web sites". Try to start with MSDN before you waste time on them.
—SA

更多推荐

C#XML Reader简化版有帮助吗?

本文发布于:2023-10-27 00:14:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531759.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简化版   XML   Reader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!