62,271
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<Name ID="J1">Jone</Name>
<Age>22</Age>
<EmpNo>F7400144</EmpNo>
<College ID="Col1">深圳XX</College>
<Address>湖南岳阳</Address>
<Date>2010-07-11</Date>
</Table>
<NewDataSet>
<NewDataSet>
<Table>
<Name ID="J2">Slice</Name>
<Age>23</Age>
<EmpNo>F7400166</EmpNo>
<College ID="Col2">深圳XX</College>
<Address>湖南</Address>
<Date>2010-07-11</Date>
</Table>
<NewDataSet>
$.ajax({
url:"XMLFile.xml",
dataType:"xml",
success:function(result)
{
$(result).find("ceshi>NewDataSet>Table").each(function(){
$(this).find("Name").each(function(){
if($(this).attr("ID")=="J1")
alert($(this).text());
})
$(this).find("College").each(function(){
if($(this).attr("ID")=="Col1")
alert($(this).text());
})
});
},
error:function(res){alert(res);}
})
public static string GetXmlValue(string xmlContent, string xmlKey)
{
string xmlValue = "";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlContent);
XmlNode xnode = xmlDoc.DocumentElement;
foreach (XmlNode node in xnode.ChildNodes)
{
if (node.Attributes[0].Value == xmlKey)
{
xmlValue = node.InnerText;
}
}
return xmlValue;
}
void Main()
{
string xml = @"<?xml version='1.0' standalone='yes'?>
<NewDataSet>
<Table>
<Name ID='J1'>Jone</Name>
<Age>22</Age>
<EmpNo>F7400144</EmpNo>
<College ID='Col1'>深圳XX</College>
<Address>湖南岳阳</Address>
<Date>2010-07-11</Date>
</Table>
<Table>
<Name ID='J2'>Slice</Name>
<Age>23</Age>
<EmpNo>F7400166</EmpNo>
<College ID='Col2'>深圳XX</College>
<Address>湖南</Address>
<Date>2010-07-11</Date>
</Table>
</NewDataSet>";
XElement page = XElement.Parse(xml,LoadOptions.SetLineInfo);
var result =page.Descendants("Table").Where(e => new string[] { "J1", "Col1" }.Contains((string)e.Element("Name").Attribute("ID")));
foreach (var q in result)
{
Console.WriteLine("Element: {0} : Value = {1} ",q.Name,q.Value);
}
}
// 结果:
//Element: Table : Value = Jone22F7400144深圳XX湖南岳阳2010-07-11