Solved

Trouble creating an xml file in C#

Posted on 2013-06-13
9
347 Views
Last Modified: 2013-06-21
Hey guys!!

I'm having trouble creating an xml file in C#.

XmlTextWriter writer = new XmlTextWriter("C:\\test.xml", Encoding.UTF8);
            writer.WriteStartDocument();
            writer.WriteRaw("<Message xmlns="http://www.surescripts.com/messaging">");
            //I can't seem to make this work. Should be <Message xmlns="http://www.surescripts.com/messaging">

            writer.WriteStartElement("root");
            // I don't want to print Root in my document.
           
            writer.WriteStartElement("Header");
            writer.WriteElementString("To","mailto:4736190679001.spi@surescripts.com");
            writer.WriteElementString("From","mailto:1111111.ncpdp@surescripts.com");
            writer.WriteElementString("MessageID", "333c32da7169436fbc703153bd722d34");
            writer.WriteElementString("SentTime", "2006-06-23T21:19:04.9Z");
            writer.WriteEndElement();


            writer.WriteStartElement("Body");
            writer.WriteElementString("Refill Request", "");
            writer.WriteElementString("RxReferenceNumber", "FMEM-999512");
            writer.WriteElementString("PrescriberOrderNumber","ORD-888512");

            writer.WriteStartElement("Pharmacy");
            writer.WriteStartElement("Identification");
            writer.WriteElementString("StoreName", "C 4");
            writer.WriteEndElement();
            writer.WriteStartElement("Address");
            writer.WriteElementString("AddressLine1", "500 W Commercial St");
            writer.WriteElementString("City", "Ozark");
            writer.WriteElementString("State", "AR");
            writer.WriteElementString("ZipCode", "72949");
            writer.WriteEndElement();

            writer.WriteStartElement("PhoneNumbers");
            writer.WriteStartElement("Phone");
            writer.WriteElementString("Number", "4796672101");
            writer.WriteElementString("Qualifier", "TE");
            writer.WriteEndElement();
            writer.WriteStartElement("Phone");
            writer.WriteElementString("Number", "4796671605");
            writer.WriteElementString("Qualifier", "FX");
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement("Prescriber");
            writer.WriteStartElement("Identification");
            writer.WriteElementString("SPI", "580805988");
            writer.WriteEndElement();
            writer.WriteStartElement("Name");
            writer.WriteElementString("Last Name", "Mouse");
            writer.WriteElementString("First Name", "Mickey");
            writer.WriteElementString("Middle Name", "R.");
            writer.WriteElementString("Suffix", "M.D.");
            writer.WriteElementString("Prefix", "Dr.");
            writer.WriteEndElement();
            writer.WriteStartElement("Address");
            writer.WriteElementString("Address Line 1", "30 Hunter Lane");
            writer.WriteElementString("City", "Carbon City");
            writer.WriteElementString("State", "AR");
            writer.WriteElementString("ZipCode", "72955");
            writer.WriteEndElement();

            writer.WriteStartElement("PhoneNumbers");
            writer.WriteStartElement("Phone");
            writer.WriteElementString("Number", "4796672101");
            writer.WriteElementString("Qualifier", "TE");
            writer.WriteEndElement();
            writer.WriteStartElement("Phone");
            writer.WriteElementString("Number", "4796671605");
            writer.WriteElementString("Qualifier", "FX");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
           
            writer.WriteStartElement("Patient");
            writer.WriteStartElement("Identification");
            writer.WriteElementString("SocialSecurity", "1234567890");
            writer.WriteEndElement();
            writer.WriteStartElement("Name");
            writer.WriteElementString("LastName", "Mouse");
            writer.WriteElementString("FirstName", "Minnie");
            writer.WriteElementString("MiddleName", "G.");
            writer.WriteEndElement();
            writer.WriteElementString("Gender", "F");
            writer.WriteElementString("DateOfBirth", "19350101");
            writer.WriteStartElement("Address");
            writer.WriteElementString("Address Line 1", "1922 N Carbon City Rd");
            writer.WriteElementString("City", "Carbon City");
            writer.WriteElementString("State", "AR");
            writer.WriteElementString("ZipCode", "72955");
            writer.WriteEndElement();
            writer.WriteStartElement("PhoneNumbers");
            writer.WriteStartElement("Phone");
            writer.WriteElementString("Number", "4796332101");
            writer.WriteElementString("Qualifier", "TE");
            writer.WriteEndElement();
            writer.WriteStartElement("Phone");
            writer.WriteElementString("Number", "4796331605");
            writer.WriteElementString("Qualifier", "FX");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
           
            writer.WriteStartElement("Medication Prescribed");
            writer.WriteElementString("DrugDescription", "Amoxicillin 250mg/5ml Suspension");
            writer.WriteStartElement("DrugCoded");
            writer.WriteElementString("ProductCode", "00003173840");
            writer.WriteElementString("ProductCodeQualifier", "ND");
            writer.WriteElementString("DosageForm", "61");
            writer.WriteEndElement();

            writer.WriteStartElement("Quantity");
            writer.WriteElementString("Qualifier", "ME");
            writer.WriteElementString("Value", "60");
            writer.WriteEndElement();

            writer.WriteStartElement("Directions", "Take one teaspoonsful by mouth daily.");
            writer.WriteEndElement();
            //Should be <Directions>Take one teaspoonsful by mouth daily.</Directions>
            writer.WriteRaw("Note />");

            writer.WriteStartElement("Refills");
            writer.WriteElementString("Qualifier", "R");
            writer.WriteElementString("Quantity", "02");
            writer.WriteEndElement();

            writer.WriteStartElement("Substitutions", "0"); writer.WriteEndElement();
            //  Should be <Substitutions>0</Substitutions>

            writer.WriteStartElement("WrittenDate", "20060322");
            writer.WriteEndElement();
            // Should be <WrittenDate>20060322</WrittenDate>

            writer.WriteStartElement("LastFillDate", "20060324");
            writer.WriteEndElement();
            //Should be <LastFillDate>20060324</LastFillDate>


            writer.WriteEndElement();

            writer.WriteEndElement();

            writer.WriteEndElement();

            writer.WriteEndElement();


            writer.Close();


Thanks!!!

Jerry
0
Comment
Question by:JDL129
  • 6
  • 3
9 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39244185
do u have the expected xml for reference?
also u can't have element name with space inside, so this line will cause malformed xml:
writer.WriteElementString("Address Line 1", "1922 N Carbon City Rd");

Open in new window

you should use:
writer.WriteElementString("AddressLine1", "1922 N Carbon City Rd");

Open in new window

or
writer.WriteElementString("Address_Line_1", "1922 N Carbon City Rd");

Open in new window

0
 

Author Comment

by:JDL129
ID: 39245130
sedgwick!!  Thanks for the post!!

The expected information is  below in the Should  be line

Thanks

Jerry
0
 

Author Comment

by:JDL129
ID: 39245275
sedgwick!!  This is what I am trying to create:

<Message xmlns="http://www.surescripts.com/messaging">
   <Header>
      <To>mailto:4736190679001.spi@surescripts.com</To>
      <From>mailto:1111111.ncpdp@surescripts.com</From>
      <MessageID>333c32da7169436fbc703153bd722d34</MessageID>
      <SentTime>2006-06-23T21:19:04.9Z</SentTime>
   </Header>
   <Body>
      <RefillRequest>
         <RxReferenceNumber>FMEM-999512</RxReferenceNumber>
         <PrescriberOrderNumber>ORD-888512</PrescriberOrderNumber>
         <Pharmacy>
            <Identification>
               <NCPDPID>1111111</NCPDPID>
            </Identification>
            <StoreName>4 Rx</StoreName>
            <Address>
               <AddressLine1>C St</AddressLine1>
               <City>Eagle</City>
               <State>VA</State>
               <ZipCode>22602</ZipCode>
            </Address>
            <PhoneNumbers>
               <Phone>
                  <Number>3334444444</Number>
                  <Qualifier>TE</Qualifier>
               </Phone>
               <Phone>
                  <Number>4443332222</Number>
                  <Qualifier>FX</Qualifier>
               </Phone>
            </PhoneNumbers>
         </Pharmacy>
         <Prescriber>
            <Identification>
               <SPI>4736190679001</SPI>
            </Identification>
            <Name>
               <LastName>Greiner</LastName>
               <FirstName>Ann</FirstName>
               <MiddleName>G.</MiddleName>
               <Suffix>M.D.</Suffix>
               <Prefix>Dr.</Prefix>
            </Name>
            <Address>
               <AddressLine1>30 Hunter Lane</AddressLine1>
               <City>Camp Hill</City>
               <State>PA</State>
               <ZipCode>170112400</ZipCode>
            </Address>
            <PhoneNumbers>
               <Phone>
                  <Number>9175556767</Number>
                  <Qualifier>TE</Qualifier>
               </Phone>
               <Phone>
                  <Number>9175556868</Number>
                  <Qualifier>FX</Qualifier>
               </Phone>
            </PhoneNumbers>
         </Prescriber>
         <Patient>
            <Identification>
               <SocialSecurity>738473829</SocialSecurity>
            </Identification>
            <Name>
               <LastName>Richardson</LastName>
               <FirstName>Douglas</FirstName>
               <MiddleName>R.</MiddleName>
            </Name>
            <Gender>M</Gender>
            <DateOfBirth>19980921</DateOfBirth>
            <Address>
               <AddressLine1>23 Trumble Dr.</AddressLine1>
               <AddressLine2>Apt 101</AddressLine2>
               <City>Holyoke</City>
               <State>MA</State>
               <ZipCode>01040</ZipCode>
            </Address>
            <PhoneNumbers>
               <Phone>
                  <Number>4137760938</Number>
                  <Qualifier>TE</Qualifier>
               </Phone>
               <Phone>
                  <Number>4137762738</Number>
                  <Qualifier>NP</Qualifier>
               </Phone>
            </PhoneNumbers>
         </Patient>
         <MedicationPrescribed>
            <DrugDescription>Amoxicillin 250mg/5ml Suspension</DrugDescription>
            <DrugCoded>
               <ProductCode>00003173840</ProductCode>
               <ProductCodeQualifier>ND</ProductCodeQualifier>
               <DosageForm>61</DosageForm>
            </DrugCoded>
            <Quantity>
               <Qualifier>ME</Qualifier>
               <Value>0060</Value>
            </Quantity>
            <Directions>Take one teaspoonsful by mouth daily.</Directions>
            <Note />
            <Refills>
               <Qualifier>R</Qualifier>
               <Quantity>02</Quantity>
            </Refills>
            <Substitutions>0</Substitutions>
            <WrittenDate>20060322</WrittenDate>
            <LastFillDate>20060324</LastFillDate>
         </MedicationPrescribed>
      </RefillRequest>
   </Body>
</Message>


Thanks for your help!!!

Jerry
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:JDL129
ID: 39251874
Sedgwick!!!

Is there a problem?

Thanks,
Jerry
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252081
Which .net version u use?
0
 

Author Comment

by:JDL129
ID: 39252252
C#
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252338
.net version not language.
2.0/3.5/4.0?
0
 

Author Comment

by:JDL129
ID: 39253205
Sorry!! 3.5.
0
 

Author Closing Comment

by:JDL129
ID: 39267272
Thanks Mr. Sedgewick!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Single Form 8 42
Page navigation in windows phone 8.1 application not working. 5 24
Reset asp.net login password 4 42
Where did System.Data.Objects go? 2 23
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now