各位大虾求教解析XML,谢谢

wangdiyao 2011-02-10 04:17:29

<Field>
<!-- Name of Stencil the condition is recorded on. Defined by BCA -->
<FieldName>StencilName</FieldName>
<FieldValue>Car - Internal - Portrait</FieldValue>
</Field>
<Field>
<!-- X coordinate of condition item -->
<FieldName>X</FieldName>
<FieldValue>123</FieldValue>
</Field>
<Field>
<!-- Y coordinate of condition item -->
<FieldName>Y</FieldName>
<FieldValue>233</FieldValue>
</Field>

如何读取X和Y的值
...全文
102 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
拿凤姐抵债 2011-02-11
  • 打赏
  • 举报
回复
LinQ很强啊
tigerleq 2011-02-10
  • 打赏
  • 举报
回复
string xpath = "/Field/FieldName[@FieldName='X']";
doc.GetElement(xpath )[0];


学学xpath(一般的定位最少要学会这个)或xquery吧
对xml操作很牛
http://topic.csdn.net/u/20100514/10/ab04de54-af37-4be6-ad70-c7b3f7214c91.html
看看这个,或许对你有帮组



wuyq11 2011-02-10
  • 打赏
  • 举报
回复
123
233
wuyq11 2011-02-10
  • 打赏
  • 举报
回复
string xml = @"<root>
<Field>
<!-- Name of Stencil the condition is recorded on. Defined by BCA -->
<FieldName>StencilName</FieldName>
<FieldValue>Car - Internal - Portrait</FieldValue>
</Field>
<Field>
<!-- X coordinate of condition item -->
<FieldName>X</FieldName>
<FieldValue>123</FieldValue>
</Field>
<Field>
<!-- Y coordinate of condition item -->
<FieldName>Y</FieldName>
<FieldValue>233</FieldValue>
</Field>
</root>";
XElement root = XElement.Parse(xml);
var query = root.Descendants("Field").Where(x => new string[] { "X", "Y" }.Contains(x.Element("FieldName").Value)).Select(x => x.Element("FieldValue").Value).ToList();
foreach (var q in query)
{
Console.WriteLine(q);
}
mengqiu_hu 2011-02-10
  • 打赏
  • 举报
回复
最外面加一层/root
XmlDocument xmldoc=new XmlDocument ();
xmldoc.Load("");//加载xml,""中为xml文档的路径,或者如一楼的将xml文档定义成string xml
加载就得写成xml.LoadXml(xml);
XmlNode xmlnode=xml.SelectSingleNode("/root");
XmlNodeList xnlNodelist=xmlnode.ChildNodes;
foreach(XmlNode item in xmlNodelist)
{
XmlNode xn=item.FirstChild;
string nodeValue=xn.InnerText;
}
Valefish 2011-02-10
  • 打赏
  • 举报
回复
格式不正确
mengqiu_hu 2011-02-10
  • 打赏
  • 举报
回复
你给的格式不正确吧,外面好像应该还有一层,根节点不能是多个
q107770540 2011-02-10
  • 打赏
  • 举报
回复

void Main()
{
string xml = @"<root>
<Field>
<!-- Name of Stencil the condition is recorded on. Defined by BCA -->
<FieldName>StencilName</FieldName>
<FieldValue>Car - Internal - Portrait</FieldValue>
</Field>
<Field>
<!-- X coordinate of condition item -->
<FieldName>X</FieldName>
<FieldValue>123</FieldValue>
</Field>
<Field>
<!-- Y coordinate of condition item -->
<FieldName>Y</FieldName>
<FieldValue>233</FieldValue>
</Field></root>
";
XElement xmlPage = XElement.Parse(xml);

var query = from x in xmlPage.Descendants("Field")
where new string[]{"X","Y"}.Contains(x.Element("FieldName").Value)
select x.Element("FieldValue").Value;

foreach(var q in query)
{
Console.WriteLine(q);
}
/*
123
233

*/
}

110,578

社区成员

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

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

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