.net读取xml 文件问题

新亿 2009-10-19 02:35:43
xml文件是这样的
<?xml version="1.0" encoding="utf-8"?>
<Map mapName="中文名" mapCode="英文名" mapWidth="1000" mapHeight="1000">
<tile>
<x="0" y="0" type="city" cityName="北京"/>
<x="1" y="0" type="wild" cityName="上海"/>
</tile>
</Map>

要求读出 x,y,type,cityname 等属性的值 <x 有多少就输出多少 好久没写xml操作了 怎么取。。?
...全文
46 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenery 2009-10-19
  • 打赏
  • 举报
回复
你XML的节点有问题
static void t1()
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Map mapName=""中文名"" mapCode=""英文名"" mapWidth=""1000"" mapHeight=""1000"">
<tile>
<item x=""0"" y=""0"" type=""city"" cityName=""北京""/>
<item x=""1"" y=""0"" type=""wild"" cityName=""上海""/>
</tile>
</Map>
";
XmlDocument doc=new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode e in doc.SelectNodes("//item"))
{
Console.WriteLine(string.Format("x={0},y={1}",e.Attributes["x"].Value,e.Attributes["y"].Value));
}
}
starsword 2009-10-19
  • 打赏
  • 举报
回复
用 DataSet.ReadXml(filename) 把 XML 内容读到 DataSet 中,然后就可以用 DataSet.Tables[0].Rows[0]["x"] 这样读出属性值了

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧