0

I am new to XML I am receiving the following file/string. How can I break it in C# so I can put each of the fields in my SQL server Database? BTW I don't know how to format XML in StackOverflow if somebody can tell me how to do it. I'll do 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>
2
  • XML is formatted the same way as code: surround the XML content between two ` marks, or select the content and press the icon with the "1010" in the icon bar above the text editor. Commented Nov 6, 2009 at 15:42
  • 1
    Exact duplicate of stackoverflow.com/questions/1688024/parse-xml-string-net Commented Nov 6, 2009 at 15:45

3 Answers 3

1

look at the XmlDocument class

Also, if you are using .NET Framework 3.5 or higher, you can use the XDocument class.

Sign up to request clarification or add additional context in comments.

1 Comment

I think it needs to be a smidge more emphatic than that - if you can use XDocument and Linq to XML then that should almost certainly be your default choice...
0

The XML was formatted wrong by the site, so I'll just assume it is some valid XML document in a single string.

If that is the case, look at the XmlDocument class, you can feed your string to it, the class will parse it, and you can then extract your values so that you can put them in the database as appropriate.

Comments

0

The xml has to be valid of course.

  • In case you use datasets and datatables to persist your data you can use the DataSet in this way.
  • A better way in my oppinion is to use objects, in that case you can use the xml serializer, that serializes your xml directly to objects. This does take a bit more work to get done, and a little more searching to get the serialize attributes correct, but it is worth it in my oppinion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.