具有多个元素拆分的Xml [英] Xml with multple element split

查看:60
本文介绍了具有多个元素拆分的Xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的XML字符串:



 <  < span class =code-leadattribute>标题 >  
< Fr >
< ID >
< Fin >
< Clr >
< MId > 002 < / MId >
< / Clr >
< / Fin >
< / Id >
< / Fr >
< > ;
< Id >
< Fin >
< Clr >
< < span class =code-leadattribute> MId > 025 < / MId >
< < span class =code-leadattribute> / Clr >
< / Fin >
< / Id >
< ; /至 >
< Msgid > 93734 < / Msgid >
< / Header >
< 正文 >
< F1 >
< GH >
< A > KANG201611021631893734 < / A >
< B > 2016-11-02T04:11:52 < / B >
< ; C > 1 < / C >
< ; D Ccy < span class =code-attribute> = INR > 2500.00 < / D >
< E > 2016-11-02 < / E >
< / GH >
< CD >
< P >
< E1 > / XUTR / KANGH16307631893734 < / E1 >
< T1 > KANGR5 20161102631893734 < / T1 >
< / P >
< P2 >
< I1 > HIGH < / I1 >
< S1 >
< P2 > asd < / P2
>
< / S1 >
< L1 >
< P4 > werwer < / P4 >
< / L1 >
< C6 >
< cc > 100 < / cc >
< / C6 >
< / P2 >
< / CD >
< < span class =code-leadattribute> / F1 >
< / Body >







在这个XML中,像Header和Body这样的多个元素

i希望将这个单个字符串拆分为2个为separ吃头和身体。



我尝试过:



< pre lang =c#> string xml = < ; root> + messageStream + < / root>;
XDocument doc = XDocument.Parse(xml);
// 字符串状态= doc.Root.Element(标题)。值;

解决方案

 XElement header = doc.Element(  标题); 
XElement body = doc.Element( Body);





如果你想把它们作为字符串,那么事后再做:



  string  headerStr = header.ToString(); 
string bodyStr = body.ToString();


Below is my XML string :

<Header>
    <Fr>
        <Id>
            <Fin>
                <Clr>
                    <MId>002</MId>
                </Clr>
            </Fin>
        </Id>
    </Fr>
    <To>
        <Id>
            <Fin>
                <Clr>
                    <MId>025</MId>
                </Clr>
            </Fin>
        </Id>
    </To>
    <Msgid>93734</Msgid>
</Header>
<Body>
    <F1>
        <GH>
            <A>KANG201611021631893734</A>
            <B>2016-11-02T04:11:52</B>
            <C>1</C>
            <D Ccy = "INR">2500.00</D>
            <E>2016-11-02</E>
        </GH>
        <CD>
            <P>
                <E1>/XUTR/KANGH16307631893734</E1>
                <T1>KANGR520161102631893734</T1>
            </P>
            <P2>
                <I1>HIGH</I1>
                <S1>
                    <P2>asd</P2>
                </S1>
                <L1>
                    <P4>werwer</P4>
                </L1>
                <C6>
                    <cc>100</cc>
                </C6>
            </P2>
        </CD>
    </F1>
</Body>




In this XML Multiple elements like "Header" and "Body"
i want to split this single string to two as separate Header and Body .

What I have tried:

string xml = "<root>" + messageStream + "</root>";
XDocument doc = XDocument.Parse(xml); 
//String status = doc.Root.Element("Header").Value;

解决方案

XElement header = doc.Element("Header");
XElement body = doc.Element("Body");



If you want them as strings, do this afterwards:

string headerStr = header.ToString();
string bodyStr = body.ToString();


这篇关于具有多个元素拆分的Xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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