Solved

Trouble creating an xml file in C#

Posted on 2013-06-13
9
340 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
Comment Utility
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
Comment Utility
sedgwick!!  Thanks for the post!!

The expected information is  below in the Should  be line

Thanks

Jerry
0
 

Author Comment

by:JDL129
Comment Utility
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
 

Author Comment

by:JDL129
Comment Utility
Sedgwick!!!

Is there a problem?

Thanks,
Jerry
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
Which .net version u use?
0
 

Author Comment

by:JDL129
Comment Utility
C#
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
.net version not language.
2.0/3.5/4.0?
0
 

Author Comment

by:JDL129
Comment Utility
Sorry!! 3.5.
0
 

Author Closing Comment

by:JDL129
Comment Utility
Thanks Mr. Sedgewick!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

11 Experts available now in Live!

Get 1:1 Help Now