C#XML Reader简化版有帮助吗? [英] C# XML Reader simplified version help?
本文介绍了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,因为它不包含硬编码,并且是最不介入的.您只需根据合同持久化数据对象的任何对象图即可.
参见:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].
另请参阅我过去的答案,其中我主张采用这种可靠的方法:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [ http://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:
http://msdn.microsoft.com/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 toXmlReader
, the best reading is this: http://msdn.microsoft.com/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简化版有帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文