在C#中使用XML

在C#中使用XML

问题描述:

您好,

我尝试从XML获取特定的子父级并将其解析为单独的XDocument时遇到了一些问题。

例如这里是漏洞XML:

Hello,
I have a bit problem trying to get specific sub-parent from XML and parse it as separate XDocument.
For example here is the hole XML:

<?xml version='1.0' encoding="UTF-8" standalone="no"?>
<OPS_envelope>
    <header>
        <version>0.9</version>
    </header>
    <body>
        <data_block>
            <dt_assoc>
                <item key="protocol">XCP</item>
                <item key="object">DOMAIN</item>
                <item key="response_text">Query Successful</item>
                <item key="action">REPLY</item>
                <item key="attributes">
                    <dt_assoc>
                        <item key="auto_renew">1</item>
                        <item key="contact_set">
                            <dt_assoc>
                                <item key="admin">
                                    <dt_assoc>
                                        <item key="country">US</item>
                                        <item key="address3">Admin</item>
                                        <item key="org_name">Example Inc.</item>
                                        <item key="phone">+1.4165550123x1812</item>
                                        <item key="last_name">Adams</item>
                                        <item key="address2">Suite 100</item>
                                      	<item key="status">active</item>
                                        <item key="state">CA</item>
                                        <item key="email">adams@example.com</item>
                                        <item key="city">Santa Clara</item>
                                        <item key="postal_code">90210</item>
                                        <item key="fax">+1.4165550125</item>
                                        <item key="address1">32 Oak Street</item>
                                        <item key="first_name">Adler</item>
                                    </dt_assoc>
                                </item>
                                <item key="owner">
                                    <dt_assoc>
                                        <item key="country">US</item>
                                        <item key="address3">Owner</item>
                                      	<item key="status">active</item>
                                        <item key="org_name">Example Inc.</item>
                                        <item key="phone">+1.4165550123x1902</item>
                                        <item key="last_name">Ottway</item>
                                        <item key="address2">Suite 500</item>
                                        <item key="state">CA</item>
                                        <item key="email">ottway@example.com</item>
                                        <item key="city">SomeCity</item>
                                        <item key="postal_code">90210</item>
                                        <item key="fax">+1.4165550124</item>
                                        <item key="address1">32 Oak Street</item>
                                        <item key="first_name">Owen</item>
                                    </dt_assoc>
                                </item>
                                <item key="billing">
                                    <dt_assoc>
                                        <item key="country">US</item>
                                        <item key="address3">Billing</item>
                                        <item key="org_name">Example Inc.</item>
                                        <item key="phone">+1.4165550123x1248</item>
                                        <item key="last_name">Burton</item>
                                        <item key="address2">Suite 200</item>
                                      	<item key="status">active</item>
                                        <item key="state">CA</item>
                                        <item key="email">burton@example.com</item>
                                        <item key="city">Santa Clara</item>
                                        <item key="postal_code">90210</item>
                                        <item key="fax">+1.4165550136</item>
                                        <item key="address1">32 Oak Street</item>
                                        <item key="first_name">Bill</item>
                                    </dt_assoc>
                                </item>
                            </dt_assoc>
                        </item>
                        <item key="registry_updatedate">2016-04-22 05:36:05</item>
                        <item key="sponsoring_rsp">1</item>
                        <item key="registry_createdate">2010-01-03 06:48:18</item>
                        <item key="affiliate_id"></item>
                        <item key="expiredate">2017-01-03 06:48:18</item>
                        <item key="let_expire">0</item>
                        <item key="tld_data"></item>
                        <item key="registry_expiredate">2017-01-03 06:48:18</item>
                        <item key="nameserver_list">
                            <dt_array>
                                <item key="0">
                                    <dt_assoc>
                                        <item key="ipaddress">21.40.33.21</item> <item key="sortorder">1</item>
                                        <item key="name">ns1.systemdns.com</item>
                                    </dt_assoc>
                                </item>
                                <item key="1">
                                    <dt_assoc>
                                        <item key="ipaddress">207.136.100.142</item>
                                        <item key="sortorder">2</item>
                                        <item key="name">ns2.systemdns.com</item>
                                    </dt_assoc>
                                </item>
                                <item key="2">
                                    <dt_assoc>
                                        <item key="ipaddress">24.22.23.28</item>
                                        <item key="sortorder">3</item>
                                        <item key="name">patrick.example.com</item>
                                    </dt_assoc>
                                </item>
                                <item key="3">
                                    <dt_assoc>
                                        <item key="ipaddress">24.22.23.24</item>
                                        <item key="sortorder">4</item>
                                        <item key="name">qa1.example.com</item>
                                    </dt_assoc>
                                </item>
                                <item key="4">
                                    <dt_assoc>
                                        <item key="ipaddress">24.22.23.25</item>
                                        <item key="sortorder">5</item>
                                        <item key="name">qa2.example.com</item>
                                    </dt_assoc>
                                </item>
                            </dt_array>
                        </item>
                    </dt_assoc>
                </item>
                <item key="response_code">200</item>
                <item key="is_success">1</item>
            </dt_assoc>
        </data_block>
    </body>
</OPS_envelope>





What i want to do is to get every item where key is admin, owner or billing and parse it as separate XML but so far i am able to parse only the hole XML.



What I have tried:



I am tryining



What i want to do is to get every item where key is admin, owner or billing and parse it as separate XML but so far i am able to parse only the hole XML.

What I have tried:

I am tryining

var admin = keys.Where(item => item.Attribute("key").Value == "admin").ToList();



i want to get as string only this but it is not working:


i want to get as string only this but it is not working:

<item key="admin">
                                    <dt_assoc>
                                        <item key="country">US</item>
                                        <item key="address3">Admin</item>
                                        <item key="org_name">Example Inc.</item>
                                        <item key="phone">+1.4165550123x1812</item>
                                        <item key="last_name">Adams</item>
                                        <item key="address2">Suite 100</item>
                                      	<item key="status">active</item>
                                        <item key="state">CA</item>
                                        <item key="email">adams@example.com</item>
                                        <item key="city">Santa Clara</item>
                                        <item key="postal_code">90210</item>
                                        <item key="fax">+1.4165550125</item>
                                        <item key="address1">32 Oak Street</item>
                                        <item key="first_name">Adler</item>
                                    </dt_assoc>
                                </item>



Any help or advice?


Any help or advice?

I found a solution. By help from one guy from stacoverflow we create function that is getting the specific part of the XML as string.

Here is the solution:



I found a solution. By help from one guy from stacoverflow we create function that is getting the specific part of the XML as string.
Here is the solution:

public string ExtractItem(string response, string key) 
{ 
var xmlDoc = XDocument.Parse(response); 
var element = xmlDoc.Descendants("item").FirstOrDefault(i => i.Attribute("key").Value == key); 
return element.ToString(); 
}