C# 有条件地循环遍历XML文件,仅在元素与值相等时读取数据
我有以下XML:-C# 有条件地循环遍历XML文件,仅在元素与值相等时读取数据,c#,xml,C#,Xml,我有以下XML:- <Scripts> <ParseCheck>TEST</ParseCheck> <COMMON> <DBConnections> <Connection name = "Book"> <Instance>SERVER1</Instance> <DB>DB1</DB>
<Scripts>
<ParseCheck>TEST</ParseCheck>
<COMMON>
<DBConnections>
<Connection name = "Book">
<Instance>SERVER1</Instance>
<DB>DB1</DB>
<Provider>SQLOLEDB</Provider>
<AuthType>Windows</AuthType>
<User></User>
<Pwd></Pwd>
</Connection>
<Connection name = "Report">
<Instance>SERVER2</Instance>
<DB>DB2</DB>
<Provider>SQLOLEDB</Provider>
<AuthType>Windows</AuthType>
<User></User>
<Pwd></Pwd>
</Connection>
</DBConnections>
</COMMON>
试验
服务器1
DB1
SQLOLEDB
窗户
服务器2
DB2
SQLOLEDB
窗户
到目前为止,我掌握的代码是:-
while (xmlreader.Read())
{
switch (xmlreader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
//Console.Write("<" + xmlreader.Name);
while (xmlreader.MoveToNextAttribute())
if (xmlreader.Value != "Book")
{
continue;
}
else
{
Console.Write(" " + xmlreader.Name + "='" + xmlreader.Value + "'");
}
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(xmlreader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + xmlreader.Name);
Console.WriteLine(">");
break;
}
}
while(xmlreader.Read())
{
开关(xmlreader.NodeType)
{
case XmlNodeType.Element://节点是一个元素。
//使用xml linq编写(“”)您可以使用字典获取连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Dictionary<string, Connection> dict = doc.Descendants("Connection")
.Select(x => new Connection() {
name = (string)x.Attribute("name"),
instance = (string)x.Element("Instance"),
db = (string)x.Element("DB"),
provider = (string)x.Element("Provider"),
authType = (string)x.Element("AuthType"),
user = (string)x.Element("User"),
pwd = (string)x.Element("Pwd"),
})
.GroupBy(x => x.name, y => y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
Connection report = dict["Report"];
}
}
public class Connection
{
public string name { get; set; }
public string instance { get; set; }
public string db { get; set; }
public string provider { get; set; }
public string authType { get; set; }
public string user { get; set; }
public string pwd { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Dictionary dict=文档子体(“连接”)
.Select(x=>newconnection(){
名称=(字符串)x.Attribute(“名称”),
实例=(字符串)x.Element(“实例”),
db=(字符串)x.Element(“db”),
provider=(字符串)x.Element(“provider”),
authType=(字符串)x.Element(“authType”),
user=(string)x.Element(“user”),
pwd=(字符串)x.Element(“pwd”),
})
.GroupBy(x=>x.name,y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
连接报告=dict[“报告”];
}
}
公共类连接
{
公共字符串名称{get;set;}
公共字符串实例{get;set;}
公共字符串db{get;set;}
公共字符串提供程序{get;set;}
公共字符串authType{get;set;}
公共字符串用户{get;set;}
公共字符串pwd{get;set;}
}
}
太棒了,谢谢。我如何循环变量报告并获得每个元素所需的数据?非常感谢。