读取XML文件并将值写入TextBoxes和C# [英] Reading an XML File And Writing Values to TextBoxes and Such C#
本文介绍了读取XML文件并将值写入TextBoxes和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经把它的大部分都放下了,事实上我可以一次性读取每个值。
问题,是XML文件是怎样的是设置(我无法更改,因为这是它的应用程序如何使用它)
FE
< ServerSettings >
< property name = ServerPort value = 25000 / >
< property name = ServerIsPublic value = true / >
< property 名称 = ServerName value = 我的游戏主持人 / >
< property 名称 = ServerPassword < span class =code-attribute> value = / >
< property name = ServerMaxPlayerCount value = 4 / >
< / ServerSettings >
还有更多的值,但这只是一个例子。我需要让我的应用程序将值写入文本框。
这是我的代码,可能会使用一些改进。谢谢!
使用(XmlReader prereader = XmlReader.Create( presetlocation)) // 在使用OpenFileDialog // $ b $选择xml预设文件后声明此变量b {
while (prereader.Read())
{
if (prereader.IsStartElement())
{
switch (prereader.Name)
{
case ServerSettings:
断裂跨度>;
case property :
string attribute = prereader [ name 跨度>];
string attribute2 = prereader [ value 跨度>];
if (属性!= null )
{
// 这是将数据写入txt以测试读数。它得出了奇怪的结果//
FileStream fs1 = new FileStream( D:\\TEST.txt,FileMode.OpenOrCreate,FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.Write(attribute + + attribute2);
writer.Close();
}
break ;
}
}
}
}
解决方案
请参阅我的简短概述.NET FCL提供的XML解析方法:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。- 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。- 使用类
System.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
-SA
我解决了这个问题。我不是只搜索不等于null的所有属性,而是只搜索某些文本。
而不是
if (属性!= null )
现在是
if (attribute == ServerPort)
对不起这个愚蠢的问题:)
I've gotten most of it down, in the fact that I can read EVERY value, all at once.
The problem, is how the XML file is setup (which I can not change, as that is how the application it is for uses it)
F.E.
<ServerSettings>
<property name="ServerPort" value="25000"/>
<property name="ServerIsPublic" value="true"/>
<property name="ServerName" value="My Game Host"/>
<property name="ServerPassword" value=""/>
<property name="ServerMaxPlayerCount" value="4"/>
</ServerSettings>
There are more values and such, but this is just an example. I need to get my application to write the values to textboxes.
Here is my code, which could probably use some improvements. Thank you!
using (XmlReader prereader = XmlReader.Create(presetlocation)) // This variable is declared after selecting the xml preset file with OpenFileDialog //
{
while (prereader.Read())
{
if (prereader.IsStartElement())
{
switch (prereader.Name)
{
case "ServerSettings":
break;
case "property":
string attribute = prereader["name"];
string attribute2 = prereader["value"];
if (attribute != null)
{
// This was writing the data to a txt to test the reading. It came up with strange results //
FileStream fs1 = new FileStream("D:\\TEST.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.Write(attribute + " " + attribute2);
writer.Close();
}
break;
}
}
}
}
解决方案
Please see my short overview of the approaches to XML parsing offered by .NET FCL:
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].- Use the class
System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].- Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
—SA
I fixed the problem. Instead of just searching for ALL properties that don't equal null, I made it so it only searches for certain text.
Instead of
if (attribute != null)
It is now
if (attribute == "ServerPort")
Sorry for the stupid question :)
这篇关于读取XML文件并将值写入TextBoxes和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文