Solved

Trouble creating an xml file in C#

Posted on 2013-06-13
9
345 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
 

Author Comment

by:JDL129
ID: 39251874
Sedgwick!!!

Is there a problem?

Thanks,
Jerry
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bulid json format 3 46
Problem of RegEx to match the first occurence of 10 35
Syntax error 9 45
Anyway to make "All" the default in the dropdown? 6 34
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

910 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

27 Experts available now in Live!

Get 1:1 Help Now