Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

parsing - parse XML string .net

I have the following XML string that I need to parse(break). Any body know what is the code to be used? I can provide my code if need it.

<?xml version='1.0' encoding='ISO-8859-1'?>
<SystemGenerator-Document>
    <TN>42</TN>
    <OC>CR</OC>
    <HN>738</HN>
    <USERID>xxx</USERID>
    <WS>FACTORY</WS>
    <OBJID>254209</OBJID>
    <SystemGenerator-Process>
        <RNO>247989</RNO>
        <RSNO>1</RSNO>
        <OBJID>254209</OBJID>
        <ARR>03.11.2009</ARR>
        <DEP>21.11.2009</DEP>
        <NOPAX>2</NOPAX>
        <RT>1</RT>
        <SystemGenerator-Person>
            <ARR>03.11.2009</ARR>
            <DEP>21.11.2009</DEP>
            <PCIID>700842</PCIID>
            <HASPREV>FALSE</HASPREV>
            <HASSUCC>FALSE</HASSUCC>
            <NOPAX>1</NOPAX>
            <SF>N</SF>
            <GID>535372</GID>
            <SN>Torres</SN>
            <CN>Xavier</CN>
            <LN></LN>
            <VIP></VIP>
            <STREET></STREET>
            <CITY></CITY>
            <ZIP></ZIP>
            <COUNTRY></COUNTRY>
            <STATE></STATE>
            <AREA></AREA>
            <PHONE></PHONE>
            <PHONE2></PHONE2>
            <FAX></FAX>
            <FAX2></FAX2>
            <EMAIL></EMAIL>
            <EMAIL2></EMAIL2>
            <TAXID></TAXID>
            <DOB></DOB>
            <SEX>0</SEX>
            <PASSWD></PASSWD>
            <MATCHCODE></MATCHCODE>
            <ADMCODEHQ></ADMCODEHQ>
            <GT>GUEST</GT>
            <GTD>1</GTD>
            <GNR>19726</GNR>
            <GMD>738</GMD>
            <GDB>0</GDB>
            <TT>M</TT>
            <HQGID>0</HQGID>
            <CREQ>0</CREQ>
            <CREQSTATE>
            </CREQSTATE>
            <SALUTATION></SALUTATION>
            <TITLE></TITLE>
            <T-TITLE>
            </T-TITLE>
            <CARDS></CARDS>
            <RN>718</RN>
            <CAT></CAT>
            <TG>1A</TG>
            <MC>64</MC>
            <SystemGenerator-Package>
                <FROM>03.11.2009</FROM>
                <TO>21.11.2009</TO>
                <SID>AL</SID>
                <RS>CLG</RS>
                <SIDT>P</SIDT>
            </SystemGenerator-Package>
        </SystemGenerator-Person>
        <SystemGenerator-Person>
            <ARR>03.11.2009</ARR>
            <DEP>21.11.2009</DEP>
            <PCIID>700843</PCIID>
            <HASPREV>FALSE</HASPREV>
            <HASSUCC>FALSE</HASSUCC>
            <NOPAX>1</NOPAX>
            <SF>N</SF>
            <SN>Torres</SN>
            <CN>Xavier</CN>
            <RN>718</RN>
            <CAT></CAT>
            <TG>1A</TG>
            <MC>64</MC>
            <SystemGenerator-Package>
                <FROM>03.11.2009</FROM>
                <TO>21.11.2009</TO>
                <SID>AL</SID>
                <RS>CLG</RS>
                <SIDT>P</SIDT>
            </SystemGenerator-Package>
        </SystemGenerator-Person>
    </SystemGenerator-Process>
    <ORG>OWNER@FACTORY(3244)#4840</ORG>
</SystemGenerator-Document>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would advise using LinqToXml to parse xml data. Here is a nice Sample

Sample

public class XmlDocumentReader
{
    private XDocument xmlDoc = new XDocument();
    public XmlDocumentReader(string xml)
    {
        xmlDoc.LoadXml(xml);
    }

    public void ParseAndSave()
    {
        var document = from document in xmlDoc.Element("SystemGenerator-Document");
        select new
        {  
            TC = document.Element("TN").Value;
            OC = document.Element("OC").Value;
            HN = document.Element("HN").Value;
            USERID = document.Element("USERID").Value;
            ... and so on
        };

        // insert your data into the database
        // I would assume you have a method called "InsertDocument" or something
        MyDatabase.InsertDocument(document.TC,
                                  document.OC,
                                  document.HN,
                                  document.USERID,
                                  ...);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...