[英]Retrieve Some Data From Xml File
I have a question about reading xml. 我有一个关于阅读xml的问题。 My xml format is like this: 我的xml格式是这样的:
<?xml version="1.0" encoding="utf-8"?>
<object type="System.Windows.Forms" name="Form1">
<object>
<object type="label" name="lbl1">
<prop>firstname</prop>
</object>
<dataset>
<table name="tblOne">
<data>
<prop1>AA</prop1>
<prop2>BB</prop2>
<data>
</table>
<table name="tblTwo">
<data>
<prop1>CC</prop1>
</data>
</table>
</dataset>
<object>
<object type="textbox" name="txt1">
<prop>ABC</prop>
</object>
<dataset>
<table name="tblThree>
<data>
<prop1>DD</prop1>
<prop2>EE</prop2>
</data>
</table>
</dataSet>
</object>
I want to retrieve all data in tables Of dataset like this, 我想检索数据集表中的所有数据,如下所示,
<prop1>AA</prop1>
<prop2>BB</prop2>
<prop1>CC</prop1>
<prop1>DD</prop1>
<prop2>EE</prop2>
And then i need to check these item is prop1 or prop2 and i will insert it to some table.i am using with c#. 然后我需要检查这些项目是prop1或prop2,我将它插入到一些table.i我使用c#。 How do I read the xml? 我如何阅读xml?
To get the name/value pairs: 获取名称/值对:
var root = XElement.Parse(xml);
var pairs = (from prop in root.Descendants("data").Elements()
where prop.Name.LocalName.StartsWith("prop")
select new { Name = prop.Name.LocalName, Value = (string)prop }
).ToList();
Then just iterate and add to whatever table you indend: 然后只需迭代并添加到您要缩进的任何表中:
foreach(var pair in pairs) {
// use pair.Name and pair.Value
}
我建议你搜索谷歌搜索c# xml
。
This is hint ... It will works.... 这是暗示......它会起作用....
protected void Page_Load(object sender, EventArgs e)
{
this.data1();
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/xyz.xml"));
lblMsg.Text = ds.Tables[0].Rows[0]["data"].ToString();
}
private void data1()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/xyz.xml"));
int data = Int32.Parse(ds.Tables[0].Rows[0]["data"].ToString());
hits += 1;
ds.Tables[0].Rows[0]["data"] = data.ToString();
ds.WriteXml(Server.MapPath("~/xyz.xml"));
}
You can use xpath. 你可以使用xpath。 '//data' expression will return all data elements '// data'表达式将返回所有数据元素
XmlDocument xml = new XmlDocument();
xml.LoadXml(str); // replace str with xml
XmlNodeList xnList = xml.SelectNodes("//data");
foreach (XmlNode xn in xnList)
{
//Read <prop> nodes
}
Manipulate XML data with XPath and XmlDocument (C#) 使用XPath和XmlDocument(C#)处理XML数据
try this 尝试这个
var doc = XDocument.Parse(sourceText);
var result = doc.Descendants("data").Descendants();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.