C# winform 读取XML文件

<?xml version="1.0" encoding="UTF-8"?> <tns:serverpush xmlns:tns="http://www.bluedream.com/bean/envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bluedream.com/bean/envelope envelope.xsd "> <command> <id></id> <type></type> <flag></flag> <time></time> <from></from> </command> <data> <text> <?xml version="1.0" encoding="UTF-8"?> <tns:warninginfo xmlns:tns="http://www.bluedream.com/bean/warning" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bluedream.com/bean/warning warning.xsd "> <warningname></warningname> <warninglevel></warninglevel> <warningtype></warningtype> <warningstate></warningstate> <memo></memo> <bumen></bumen> </tns:warninginfo> </text> </data> </tns:serverpush> 这样一个XML文件,我想获得tns:warninginfo的值,也就是warningino,怎么做?

第1个回答  2020-05-10
using
System;
using
System.Xml;
/*
*
*
传入参数:CfgData
*
参数:地址、端口、用户、密码
*
*
*
方法:
GetConfig,获取配置文件
*
*/
namespace
Config
{
public
class
ConfigXML
{
private
string
xmlname;
public
ConfigXML(string
fn)
{
xmlname
=
fn;
}
public
void
GetConfig(CfgData
cfg)
{
XmlTextReader
xtr
=
new
XmlTextReader(xmlname);
XmlDocument
xd
=
new
XmlDocument();
xd.Load(xtr);
cfg.ip
=
xd.SelectSingleNode("Config").SelectSingleNode("SvcInfo").SelectSingleNode("SvcIP").InnerText;
string
s
=
xd.SelectSingleNode("Config").SelectSingleNode("SvcInfo").SelectSingleNode("SvcPort").InnerText;
cfg.port
=
Convert.ToInt32(s);
cfg.usr
=
xd.SelectSingleNode("Config").SelectSingleNode("SvcInfo").SelectSingleNode("User").InnerText;
cfg.pwd
=
xd.SelectSingleNode("Config").SelectSingleNode("SvcInfo").SelectSingleNode("PassWord").InnerText;
xtr.Close();
}
}
}
************************
<?xml
version="1.0"
encoding="utf-8"
?>
<Config>
<SvcInfo>
<SvcIP>127.0.0.1</SvcIP>
<SvcPort>3306</SvcPort>
<User>root</User>
<PassWord>123456</PassWord>
</SvcInfo>
</Config>
相似回答