使用C#解析XML [英] Parsing XML with C#
问题描述
我有一个XML文件,如下所示:
I have an XML file as follows:
我上传了XML文件: http://dl.dropbox.com /u/10773282/2011/result.xml .它是机器生成的XML,因此您可能需要一些XML查看器/编辑器.
I uploaded the XML file : http://dl.dropbox.com/u/10773282/2011/result.xml . It's a machine generated XML, so you might need some XML viewer/editor.
我使用此C#代码获取CoverageDSPriv/Module/*
中的元素.
I use this C# code to get the elements in CoverageDSPriv/Module/*
.
using System;
using System.Xml;
using System.Xml.Linq;
namespace HIR {
class Dummy {
static void Main(String[] argv) {
XDocument doc = XDocument.Load("result.xml");
var coveragePriv = doc.Descendants("CoverageDSPriv"); //.First();
var cons = coveragePriv.Elements("Module");
foreach (var con in cons)
{
var id = con.Value;
Console.WriteLine(id);
}
}
}
}
运行代码,我得到这个结果.
Running the code, I get this result.
hello.exe6144008016161810hello.exehello.exehello.exe81061hello.exehello.exe!17main_main40030170170010180180011190190012200200013hello.exe!107testfunctiontestfunction(int)40131505001460600158080216120120017140140018AA
我希望得到
hello.exe
61440
...
但是,我只得到一行长字符串.
However, I get just one line of long string.
- Q1:有什么问题吗?
- Q2:如何获取缺点中的元素数?我尝试了
cons.Count
,但是没有用. -
Q3:如果需要获取
<CoverageDSPriv><Module><ModuleNmae>
的嵌套值,请使用以下代码:
- Q1 : What might be wrong?
- Q2 : How to get the # of elements in cons? I tried
cons.Count
, but it doesn't work. Q3 : If I need to get nested value of
<CoverageDSPriv><Module><ModuleNmae>
I use this code :
var coveragePriv = doc.Descendants("CoverageDSPriv"); //.第一的(); var cons = coveragePriv.Elements("Module").Elements("ModuleName");
var coveragePriv = doc.Descendants("CoverageDSPriv"); //.First(); var cons = coveragePriv.Elements("Module").Elements("ModuleName");
我可以接受这一点,但是如果元素是深层嵌套的,那么我可能想要直接获取元素的方法.还有其他方法吗?
I can live with this, but if the elements are deeply nested, I might be wanting to have direct way to get the elements. Are there any other ways to do that?
var cons = coveragePriv.Elements("Module").Elements();
解决了此问题,但是对于NamespaceTable
,它再次将所有元素打印在一行中.
solves this issue, but for the NamespaceTable
, it again prints out all the elements in one line.
hello.exe
61440
0
8
0
1
6
1
61810hello.exehello.exehello.exe81061hello.exehello.exe!17main_main40030170170010180180011190190012200200013hello.exe!107testfunctiontestfunction(int)40131505001460600158080216120120017140140018
或者,Linq to XML可能是更好的解决方案,例如这篇文章.
Or, Linq to XML can be a better solution, as this post.
推荐答案
在我看来,您只有一个名为Module
的元素-因此.Value
只是向您返回整个元素的InnerText.您是要这样做吗?
It looks to me like you only have one element named Module
-- so .Value
is simply returning you the InnerText of that entire element. Were you intending this instead?
coveragePriv.Element("Module").Elements();
这将返回Module
元素的所有子元素,这似乎就是您想要的.
This would return all the child elements of the Module
element, which seems to be what your'e after.
更新:
<NamespaceTable>
是<Module>
的子元素,但是您似乎想像<Module>
一样处理它,因为您要写出每个子元素.因此,一种蛮力的方法是为<NamespaceTable>
添加另一个循环:
<NamespaceTable>
is a child of <Module>
but you appear to want to handle it similarly to <Module>
in that you want to write out each child element. Thus, one brute-force approach would be to add another loop for <NamespaceTable>
:
foreach (var con in cons)
{
if (con.Name == "NamespaceTable")
{
foreach (var nsElement in con.Elements())
{
var nsId = nsElement.Value;
Console.WriteLine(nsId);
}
}
else
{
var id = con.Value;
Console.WriteLine(id);
}
}
或者,您可能希望通过.Descendents()
将它们完全归一化:
Alternatively, perhaps you'd rather just denormalize them altogether via .Descendents()
:
var cons = coveragePriv.Element("Module").Descendents();
foreach (var con in cons)
{
var id = con.Value;
Console.WriteLine(id);
}
这篇关于使用C#解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!