使用LINQ读取XML文件
本文关键字:文件 XML 读取 LINQ 使用 | 更新日期: 2023-09-27 18:21:53
我正在尝试读取XML文件并将信息保存到Dictionary中。
这就是我的XML的样子:
<?xml version="1.0" encoding="utf-8" ?>
<testUI_root>
<!--Information for the first coded UI Test-->
<codedUITest name ="CodedUITestMethod1">
<description name ="Entering username from CSV" key = "1"/>
<description name ="Entering password from CSV" key = "2"/>
<description name ="Clicking exit" key ="3"/>
</codedUITest>
</testUI_root>
所以我正在尝试阅读并保存到字典中,这就是我尝试的方式:
//Load xml
//StringBuilder description = new StringBuilder();
int key;
Dictionary<int, string> valDic = new Dictionary<int, string>();
XDocument xdoc = XDocument.Load("XMLFile1.xml");
//Run query
var lv1s = from lv1 in xdoc.Descendants("codedUITest")
where lv1.Attribute("name").Value.Contains("CodedUITestMethod1")
select new
{
key = lv1.Descendants("key"),
value = lv1.Descendants("name")
};
//loop
foreach (var lv1 in lv1s)
{
foreach (var lv2_k in lv1.key)
{
//it's not entering into this loop
foreach (var lv2_v in lv1.value)
{
// isn't entering here either
key = Convert.ToInt32(lv2_k.Attribute("key").ToString());
valDic.Add(key, lv2_v.Attribute("name").ToString());
}
}
}
foreach (KeyValuePair<int, string> abc in valDic)
{
MessageBox.Show("Value: " + abc.ToString());
}
没有语法错误,这是一个合乎逻辑的错误。所以我的字典应该是这样的。
/*
1.Entering Username from CSV
2.Entering password from CSV
3.Clicking exit
*/
将xml保存到输出目录。(来自vs)在LinqPad中尝试了代码,"查询成功",但没有输出。我对林克几乎没有任何经验,如果这件事/错误很简单,我提前道歉。感谢
您正在寻找ToDictionary
方法
xdoc.Descendants("codedUITest")
.Where(x => x.Attribute("name").Value.Contains("CodedUITestMethod1"))
.Elements("description")
.ToDictionary(x => (int)x.Attribute("key"), x => (string)x.Attribute("name"));
使用XML序列化。使用visualstudio附带的xsd.exe,您可以将XML代码表示为以下c#业务对象:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class testUI_root {
private testUI_rootCodedUITest[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("codedUITest", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public testUI_rootCodedUITest[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class testUI_rootCodedUITest {
private testUI_rootCodedUITestDescription[] descriptionField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("description", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public testUI_rootCodedUITestDescription[] description {
get {
return this.descriptionField;
}
set {
this.descriptionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.18020")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class testUI_rootCodedUITestDescription {
private string nameField;
private string keyField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string key {
get {
return this.keyField;
}
set {
this.keyField = value;
}
}
}
然后,您可以将XML读取到一个业务对象中:
testUI_root theReturn = null;
System.Xml.Serialization.XmlSerializer s = null;
using (System.IO.FileStream fs = new System.IO.FileStream(thePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
if (fs.Length > 0) {
using (System.Xml.XmlTextReader r = new XmlTextReader(fs)) {
s = new System.Xml.Serialization.XmlSerializer(typeof(testUI_root));
theReturn = (testUI_root)s.Deserialize(r);
}
}
}
return theReturn;
然后您就有了XML的业务对象表示!从那里应该可以很容易地使用链接来搜索对象,以找到您想要的值。这只是一个如何做你想做的事情的快速模型,所以我还没有测试过,但希望它能引导你走上正确的道路。