• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

Help with xml attributes in C#

Hey guys,

I have an xml string:

<To Qualifier="D">1234567890123</To>

That I need to create in C#.

Any help would be appreciated.

Jerry
0
JDL129
Asked:
JDL129
  • 6
  • 5
  • 2
2 Solutions
 
anarki_jimbelCommented:
Tyr, e.g.:

        private void button4_Click(object sender, EventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            XmlElement ToEle = doc.CreateElement("To");
            ToEle.SetAttribute("Qualifier", "D");
            ToEle.InnerText = "1234567890123";

            System.Diagnostics.Debug.WriteLine(ToEle.OuterXml);

        }

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi JDL129;

You can use Linq to XML to create you XML element as follows:

XElement toElement = new XElement("To", 
                         new XAttribute("Qualifier", "D"), "1234567890123");

Open in new window


The above code will result in the followin

<To Qualifier="D">1234567890123</To>

But what do you intend to do with it, let us know so we can show the code for that as well.
0
 
JDL129Author Commented:
Tyr, e.g.:!!!!  Thanks for the post!!!

We are using a XmlTextWriter instance called writer and are creating an document using this.

Can you tell me how to send the string you created to a writer.WriteElementString?

Thanks,

Jerry
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoRetiredCommented:
Hi Jerry;

In your case you need to use the solution posted by @anarki_jimbel. Linq to XML is a different technology.
0
 
anarki_jimbelCommented:
Probably it is not the best idea to mix technologies - XmlDocument and XmlWriter also are two different technologies.

To write your string you may use code like below:

            using (XmlWriter writer = XmlWriter.Create("test.xml"))
            {

                writer.WriteStartElement("To");
                writer.WriteAttributeString("Qualifier", "D");
                writer.WriteString("1234567890123");
                writer.WriteEndElement();
            }

Open in new window


Output xml:

<?xml version="1.0" encoding="utf-8"?><To Qualifier="D">1234567890123</To>
0
 
JDL129Author Commented:
Hey guys!!!  Thanks for the posts!!

We tried anarki_jimbel's code and we get errors.  If we leave out the writeEndElement it will create the xml but the 1234567890123 is on the next line with no /TO tag.  If we put the WriteEndElement in it bombs with the error message: Token start attribute in state Content would result in an invalid xml document.

any ideas?

Jerry
0
 
JDL129Author Commented:
Where we get the error is when we move through the tags and start the Body tag.
0
 
anarki_jimbelCommented:
It's impossible to say without seeing entire code.

My snippet is working but it's just a small piece of code. In conjunction with other code... hmmm...  anything  may happen :).

Check that you use "writer.WriteStartElement("To")", not "writer.WriteElement("To")".

Also, try to google the error.
0
 
JDL129Author Commented:
Thanks for the post!!!!!!!!!!!!!!  Here is the complete code:

       private void button3_Click(object sender, EventArgs e)
        {

            string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz");

            XmlTextWriter writer = new XmlTextWriter("C:\\test.xml", Encoding.UTF8);
            writer.WriteStartDocument();
            writer.WriteStartElement("Message", "http://www.surescripts.com/messaging" + " xmins:xsi=Http://www/w3/org/2001/XMLScema-instance version=010 release=006" + " xsi:schemaLocation=Http//www.surescripts.com/messaging SS_SCRIPT_XML_10_6.XSD");

 //The ToQualifier and the FromQualifier are the two elements that we are having trouble with.  Any suggestions would be WONDERFUL!!

            writer.WriteElementString("ToQualifier", "''D''" + ">1234567890123");
            writer.WriteElementString("FromQualifier", "''P''" + ">1234563");
            writer.WriteElementString("MessageID", "888333");
            writer.WriteElementString("RelatesToMessageID", "991");
            writer.WriteElementString("SentTime", Now);

            writer.WriteStartElement("SenderSoftware");
            writer.WriteElementString("SenderSoftwareDeveloper", "VendorName");
            writer.WriteElementString("SenderSoftwareProduct", "XXX software");
            writer.WriteElementString("SenderSoftwareVersionRelease", "5.1");
            writer.WriteEndElement();

            writer.WriteElementString("RXReferenceNumber", "73737373");
            writer.WriteElementString("PrescriberOrderNumber", "PON123");
            writer.WriteEndElement();
           
            writer.WriteStartElement("Body");
            writer.WriteStartElement("RefillRequest");
            writer.WriteStartElement("Pharmacy");
            writer.WriteStartElement("Identification");

            writer.WriteElementString("NCPDPID","123456");
            writer.WriteElementString("NPI","1234567899");
            writer.WriteEndElement();

            writer.WriteElementString("StoreName","VILLAGE PHARMACY");
            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("CommunicationNumbers");
            writer.WriteStartElement("Communication");
            writer.WriteElementString("Number", "4796672101");
            writer.WriteElementString("Qualifier", "TE");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteStartElement("Prescriber");
            writer.WriteStartElement("Identification");
            writer.WriteElementString("StateLicenseNumber", "333444");
            writer.WriteElementString("NPI", "1294567890");
            writer.WriteEndElement();
           
            writer.WriteStartElement("Name");
            writer.WriteElementString("LastName", "JONSON");
            writer.WriteElementString("FirstName", "TIM");
            writer.WriteEndElement();

            writer.WriteStartElement("CommunicationNumber");
            writer.WriteStartElement("Communication");
            writer.WriteElementString("Number", "4796671605");
            writer.WriteElementString("Qualifier", "TE");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
                       
            writer.WriteStartElement("Patient");
           
            writer.WriteStartElement("Identification");
            writer.WriteElementString("SocialSecurity", "1234567890");
            writer.WriteEndElement();

            writer.WriteStartElement("Name");
            writer.WriteElementString("LastName", "ANDREWS");
            writer.WriteElementString("FirstName", "JOHN");
            writer.WriteEndElement();

            writer.WriteElementString("Gender", "M");
           
            writer.WriteStartElement("DateOfBirth");
            writer.WriteElementString("DateOfBirth", "1957-11-29");
            writer.WriteEndElement();
            writer.WriteEndElement();
             
            writer.WriteStartElement("MedicationPrescribed");
            writer.WriteElementString("DrugDescription", "LIPITOR 10MG TABLET");
            writer.WriteStartElement("DrugCoded");
            writer.WriteElementString("ProductCode", "00071015523");
            writer.WriteElementString("ProductCodeQualifier", "ND");
            writer.WriteEndElement();
           
            writer.WriteStartElement("Quantity");
            writer.WriteElementString("Value", "30");
            writer.WriteElementString("CodeListQualifier", "36");
            writer.WriteElementString("UnitSourceCode", "AC");
            writer.WriteElementString("PotencyUnitCode", "C48542");
            writer.WriteEndElement();
           
            writer.WriteElementString("DaysSupply", "30");
            writer.WriteElementString("Directions", "TAKE 1 TABLET BY MOUTH EVERYDAY.");
           
            writer.WriteStartElement("Refills");
            writer.WriteElementString("Qualifier", "P");
            writer.WriteElementString("Value", "1");
            writer.WriteEndElement();
           
            writer.WriteElementString("Substitutions", "1");
            writer.WriteStartElement("WrittenDate");
            writer.WriteElementString("Date", "2101-01-25");
            writer.WriteEndElement();
           

            writer.WriteEndElement();

            writer.WriteStartElement("MedicationDispensed");
            writer.WriteElementString("DrugDescription", "LIPITOR 10MG TABLET");
            writer.WriteStartElement("DrugCoded");
            writer.WriteElementString("ProductCode", "00071015523");
            writer.WriteElementString("ProductCodeQualifier", "ND");
            writer.WriteEndElement();

            writer.WriteStartElement("Quantity");
            writer.WriteElementString("Value", "30");
            writer.WriteElementString("CodeListQualifier", "36");
            writer.WriteElementString("UnitSourceCode", "AC");
            writer.WriteElementString("PotencyUnitCode", "C48542");
            writer.WriteEndElement();

            writer.WriteElementString("DaysSupply", "30");
            writer.WriteElementString("Directions", "TAKE 1 TABLET BY MOUTH EVERYDAY.");

            writer.WriteStartElement("Refills");
            writer.WriteElementString("Qualifier", "P");
            writer.WriteElementString("Value", "1");
            writer.WriteEndElement();

            writer.WriteElementString("Substitutions", "0");
            writer.WriteStartElement("WrittenDate");
            writer.WriteElementString("Date", "2101-01-25");
            writer.WriteEndElement();

            writer.WriteStartElement("LastFillDate");
            writer.WriteElementString("Date", "2101-01-25");
            writer.WriteEndElement();

            writer.WriteEndElement();

            writer.WriteEndElement();

            writer.WriteEndElement();

            writer.WriteEndElement();
           
            writer.Close();

       
        }

Thanks for the help!!!!

Jerry
0
 
anarki_jimbelCommented:
OK, I believe I know what the problem is.

You have two "root" elements, "Message" and "Body". This is not a valid case for Xml Document.

What structure are you expecting?

From your current code (with minor fix) you can get the following "kinda" xml, but I'd put it inside some root element like "Document" or whatever:

<Message>
  <To Qualifier="D">1234567890123</To>
  <From Qualifier="P">1234563</From>
  <MessageID>888333</MessageID>
  <RelatesToMessageID>991</RelatesToMessageID>
  <SentTime>2013-06-24 16:05:39 GMT+12:00</SentTime>
  <SenderSoftware><SenderSoftwareDeveloper>VendorName</SenderSoftwareDeveloper>
  <SenderSoftwareProduct>XXX software</SenderSoftwareProduct>
    <SenderSoftwareVersionRelease>5.1</SenderSoftwareVersionRelease>
  </SenderSoftware><RXReferenceNumber>73737373</RXReferenceNumber>
  <PrescriberOrderNumber>PON123</PrescriberOrderNumber>
</Message>
<Body>
  <RefillRequest>
    <Pharmacy>
      <Identification>
        <NCPDPID>123456</NCPDPID>
        <NPI>1234567899</NPI>
      </Identification>
      <StoreName>VILLAGE PHARMACY</StoreName>
      <Address>
        <AddressLine1>500 W Commercial St</AddressLine1>
        <City>Ozark</City>
        <State>AR</State>
        <ZipCode>72949</ZipCode>
      </Address>
      <CommunicationNumbers>
        <Communication>
          <Number>4796672101</Number>
          <Qualifier>TE</Qualifier>
        </Communication></CommunicationNumbers>
    </Pharmacy>
    <Prescriber>
      <Identification>
        <StateLicenseNumber>333444</StateLicenseNumber>
        <NPI>1294567890</NPI>
      </Identification>
      <Name>
          <LastName>JONSON</LastName>
          <FirstName>TIM</FirstName>
      </Name>
      <CommunicationNumber>
        <Communication>
          <Number>4796671605</Number>
          <Qualifier>TE</Qualifier>
        </Communication>
      </CommunicationNumber>
    </Prescriber>
    <Patient>
      <Identification>
        <SocialSecurity>1234567890</SocialSecurity>
      </Identification>
      <Name>
        <LastName>ANDREWS</LastName>
        <FirstName>JOHN</FirstName>
      </Name>
      <Gender>M</Gender>
      <DateOfBirth>
        <DateOfBirth>1957-11-29</DateOfBirth>
      </DateOfBirth>
    </Patient>
    <MedicationPrescribed>
      <DrugDescription>LIPITOR 10MG TABLET</DrugDescription>
      <DrugCoded>
        <ProductCode>00071015523</ProductCode>
        <ProductCodeQualifier>ND</ProductCodeQualifier>
      </DrugCoded>
      <Quantity>
        <Value>30</Value>
        <CodeListQualifier>36</CodeListQualifier>
        <UnitSourceCode>AC</UnitSourceCode>
        <PotencyUnitCode>C48542</PotencyUnitCode>
      </Quantity>
      <DaysSupply>30</DaysSupply>
      <Directions>TAKE 1 TABLET BY MOUTH EVERYDAY.</Directions>
      <Refills
        ><Qualifier>P</Qualifier>
        <Value>1</Value>
      </Refills>
      <Substitutions>1</Substitutions>
      <WrittenDate>
        <Date>2101-01-25</Date>
      </WrittenDate>
    </MedicationPrescribed>
    <MedicationDispensed>
      <DrugDescription>LIPITOR 10MG TABLET</DrugDescription>
      <DrugCoded>
        <ProductCode>00071015523</ProductCode>
        <ProductCodeQualifier>ND</ProductCodeQualifier>
      </DrugCoded>
      <Quantity>
        <Value>30</Value>
        <CodeListQualifier>36</CodeListQualifier>
        <UnitSourceCode>AC</UnitSourceCode>
        <PotencyUnitCode>C48542</PotencyUnitCode>
      </Quantity>
      <DaysSupply>30</DaysSupply>
      <Directions>TAKE 1 TABLET BY MOUTH EVERYDAY.</Directions>
      <Refills>
        <Qualifier>P</Qualifier>
        <Value>1</Value>
      </Refills>
      <Substitutions>0</Substitutions>
      <WrittenDate>
        <Date>2101-01-25</Date>
      </WrittenDate>
      <LastFillDate>
        <Date>2101-01-25</Date>
      </LastFillDate>
    </MedicationDispensed>
  </RefillRequest></Body>
0
 
anarki_jimbelCommented:
have to go... :).
0
 
JDL129Author Commented:
anarki_Jimbel!!!!!!!!  Thanks for the post!!

Attached is the sample I am trying to create.

Thanks,

Jerry
RefillRequest.txt
0
 
JDL129Author Commented:
Hey guys!!!  You are the best!!

anarki_jimbel's last post hit the nail on the head!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now