如何通过一个XDocument迭代,通过对象获取完整的XML结构,对象吗? [英] How to iterate through an XDocument, getting complete XML structure, object by object?

查看:214
本文介绍了如何通过一个XDocument迭代,通过对象获取完整的XML结构,对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的XmlDocument ,我可以用的XmlNode 遍历或将其转换为 。的XDocument ,并通过LINQ遍历它

I have an XmlDocument which I can traverse with XmlNode or convert it to a XDocument and traverse it via LINQ.

<Dataset>
    <Person>
        <PayrollNumber>1234567</PayrollNumber>
        <Surname>Smith-Rodrigez</Surname>
        <Name>John-Jaime-Winston Junior</Name>
        <Skills>
            <Skill>ICP</Skill>
            <Skill>R</Skill>
        </Skills>
        <HomePhone>08 8888 8888</HomePhone> 
        <MobilePhone>041 888 999</MobilePhone>
        <Email>curly@stooge.com</Email>
    </Person>
    <Person>
        <PayrollNumber>12342567</PayrollNumber>
        <Surname>Smith-Rodrigez</Surname>
        <Name>Steve</Name>
        <Skills>
            <Skill>Resus</Skill>
            <Skill>Air</Skill>
        </Skills>
        <HomePhone>08 8888 8888</HomePhone> 
        <MobilePhone>041 888 999</MobilePhone>
        <Email>curly@stooge.com</Email>
    </Person>
</Dataset>



问题1



我想转换该人员记录/在XML中一个业务实体对象(PO​​CO)节点。
所以我必须通过在一次一个人节点进行迭代,然后解析各个值。这最后一位是本身就很有趣,但首先我必须得到实际的人的记录。我的问题是,如果我通过各个节点选择(使用说的XMLList XmlDocoment )。

我最终被名聚集所有领域。我担心这样做的情况下,个人节点之一,是不完整的,甚至是缺失的,然后我不知道,当我经过一个缺少和业务对象聚集的字段。我会尝试和验证 - 。见问题2

I end up aggregating all fields by name. I am concerned to do this in case one of the person nodes is incomplete, or even missing and then I won't know which is missing when I pass through and aggregate the fields in to business objects. I will try and validate - see question 2.

我明白,这可以通过反射来实现,但我很感兴趣

I realize this can be done through reflection but I am interested.

我试图迭代通过Person对象:

I tried iterating through by Person object:

选项1:

foreach (XObject o in xDoc.Descendants("Person"))
{
    Console.WriteLine("Name" + o);
    // [...]
}

这让我2人记录(正确的),每个字符串化完整的XML文档 - 格式化为一个XML文档。 。上面的XML文档的只是一个子集

This gets me 2 person records (correct) each a stringified complete XML doc - formatted as an XML document. Just a subset of the above XML document.

但如何分割记录现在到单独的节点或领域 - 最好是尽可能无痛

But how to split up the record now into separate nodes or fields - preferably as painless as possible?

选项2:

foreach (XElement element in xDoc.Descendants("Person"))
{
    // [...]
}

这让我的XML节点 - 唯一值 - 每个人都在一个字符串,如:

This gets me the XML nodes - values only - for each Person all in one string, e.g.

1234567Smith- RodrigezJohn-海梅 - 温斯顿JuniorLevel 5,市中区2座,121国王威廉StNorth阿德莱德5000ICPR08 8888 8888041 888 999111111curly@stooge.comE

1234567Smith-RodrigezJohn-Jaime-Winston JuniorLevel 5, City Central Tower 2, 121 King William StNorth Adelaide 5000ICPR08 8888 8888041 888 999111111curly@stooge.comE

再次,没有太大的用处。

Again, not much use.

我可以验证一个的XDocument 很容易,有MSDN上一些很好的例子,但我想知道我怎么可以标记一个错误的记录。理想情况下,我希望能够在飞行中留下旧的背后良好的记录筛选出一个新的的XDocument 。这可能吗?

I can validate an XDocument quite easily, there are some good examples on MSDN, but I'd like to know how can I flag a wrong record. Ideally, I'd like to be able to filter the good records out to a new XDocument on the fly leaving the old ones behind. Is this possible?

推荐答案

的问题是,你只是打印出的元素为字符串。您需要编写的代码转换成一个的XElement <的;人> 进入你的业务对象。诚然,我会的期望的完整的XML,而不是被写出来 - 你确定你没有打印出 XElement.Value (其中串联所有?后代文本节点)

The problem is that you're just printing out the elements as strings. You need to write code to convert an XElement of <Person> into your business object. Admittedly I'd expect the full XML to be written out instead - are you sure you're not printing out XElement.Value (which concatenates all the descendant text nodes)?

(我不知道回答你的第二个问题 - 我建议你问它作为一个单独的问题在这里,让我们不要T得到答案的混合物在一个页面。)

(I'm not sure of the answer to your second question - I suggest you ask it as a separate question here, so that we don't get a mixture of answers in one page.)

这篇关于如何通过一个XDocument迭代,通过对象获取完整的XML结构,对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆