C#如何遍历XML中的内容并输出到datagardview中? 20
最终的效果如图中所示。XML文件如下<?xmlversion="1.0"encoding="utf-8"?><root><locationname="美国"><serve...
最终的效果如图中所示。
XML文件如下
<?xml version="1.0" encoding="utf-8" ?>
<root>
<location name="美国">
<server name="一号服务器" ipaddr="d1.cctv.com"/>
<server name="二号服务器" ipaddr="d2.cctv.com"/>
<server name="三号服务器" ipaddr="d3.cctv.com"/>
<server name="四号服务器" ipaddr="las.cctv.com"/>
<server name="五号服务器" ipaddr="udp53.cctv.com"/>
<server name="六号服务器|不限流量" ipaddr="d6.cctv.com"/>
<server name="七号服务器|不限流量" ipaddr="d9.cctv.com"/>
</location>
<location name="亚洲">
<server name="香港" ipaddr="hk1.cctv.com"/>
<server name="日本" ipaddr="jp1.cctv.com"/>
</location>
</root> 展开
XML文件如下
<?xml version="1.0" encoding="utf-8" ?>
<root>
<location name="美国">
<server name="一号服务器" ipaddr="d1.cctv.com"/>
<server name="二号服务器" ipaddr="d2.cctv.com"/>
<server name="三号服务器" ipaddr="d3.cctv.com"/>
<server name="四号服务器" ipaddr="las.cctv.com"/>
<server name="五号服务器" ipaddr="udp53.cctv.com"/>
<server name="六号服务器|不限流量" ipaddr="d6.cctv.com"/>
<server name="七号服务器|不限流量" ipaddr="d9.cctv.com"/>
</location>
<location name="亚洲">
<server name="香港" ipaddr="hk1.cctv.com"/>
<server name="日本" ipaddr="jp1.cctv.com"/>
</location>
</root> 展开
展开全部
addInList = new List<AddIn>();
if (!File.Exists(Config_File))
{
System.Console.WriteLine("不存在配置文件");
return;
}
try
{
XmlDocument document = new XmlDocument();
document.Load(Config_File);
XmlNode root = document.DocumentElement;
//取得节点列表
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode node in nodeList)
{
if (node.Name == "Addin")
{
AddIn addin = new AddIn();
XmlNodeList nodeListdetail = node.ChildNodes;
foreach (XmlNode nodedetail in nodeListdetail)
{
if (nodedetail.Name.Equals("FriendlyName"))
{
addin.FriendlyName = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("Description"))
{
addin.Description = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("Assembly"))
{
addin.Assembly = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("FullClassName"))
{
addin.FullClassName = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("BeforeLogin"))
{
addin.BeforeLogin = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("AfterLogin"))
{
addin.AfterLogin = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("ExitApp"))
{
addin.ExitApp = nodedetail.InnerText;
}
}
addInList.Add(addin);
}else if (node.Name == "LoginForm")
{
XmlNodeList nodeListdetail = node.ChildNodes;
foreach (XmlNode nodedetail in nodeListdetail)
{
if (nodedetail.Name.Equals("Assembly"))
{
LoginFormAssembly = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("FullClassName"))
{
LoginFormClassName = nodedetail.InnerText;
}
}
}
else if (node.Name == "MainForm")
{
XmlNodeList nodeListdetail = node.ChildNodes;
foreach (XmlNode nodedetail in nodeListdetail)
{
if (nodedetail.Name.Equals("Assembly"))
{
MainFormAssembly = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("FullClassName"))
{
MainFormClassName = nodedetail.InnerText;
}
}
}
}
}
希望你看得懂..
if (!File.Exists(Config_File))
{
System.Console.WriteLine("不存在配置文件");
return;
}
try
{
XmlDocument document = new XmlDocument();
document.Load(Config_File);
XmlNode root = document.DocumentElement;
//取得节点列表
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode node in nodeList)
{
if (node.Name == "Addin")
{
AddIn addin = new AddIn();
XmlNodeList nodeListdetail = node.ChildNodes;
foreach (XmlNode nodedetail in nodeListdetail)
{
if (nodedetail.Name.Equals("FriendlyName"))
{
addin.FriendlyName = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("Description"))
{
addin.Description = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("Assembly"))
{
addin.Assembly = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("FullClassName"))
{
addin.FullClassName = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("BeforeLogin"))
{
addin.BeforeLogin = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("AfterLogin"))
{
addin.AfterLogin = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("ExitApp"))
{
addin.ExitApp = nodedetail.InnerText;
}
}
addInList.Add(addin);
}else if (node.Name == "LoginForm")
{
XmlNodeList nodeListdetail = node.ChildNodes;
foreach (XmlNode nodedetail in nodeListdetail)
{
if (nodedetail.Name.Equals("Assembly"))
{
LoginFormAssembly = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("FullClassName"))
{
LoginFormClassName = nodedetail.InnerText;
}
}
}
else if (node.Name == "MainForm")
{
XmlNodeList nodeListdetail = node.ChildNodes;
foreach (XmlNode nodedetail in nodeListdetail)
{
if (nodedetail.Name.Equals("Assembly"))
{
MainFormAssembly = nodedetail.InnerText;
}
else if (nodedetail.Name.Equals("FullClassName"))
{
MainFormClassName = nodedetail.InnerText;
}
}
}
}
}
希望你看得懂..
展开全部
浏览到一个网址就是解决你这个问题的:http://tuibianzhilv001.blog.163.com/blog/static/167563877201062024821564/
进去看一下吧!
进去看一下吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上面写的 也没有如何加载到datagridview中啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询