Solved

Help with xml attributes in C#

Posted on 2013-06-23
13
488 Views
Last Modified: 2013-06-24
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
Comment
Question by:JDL129
  • 6
  • 5
  • 2
13 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39270009
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39270097
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
 

Author Comment

by:JDL129
ID: 39270132
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
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 39270138
Hi Jerry;

In your case you need to use the solution posted by @anarki_jimbel. Linq to XML is a different technology.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39270158
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
 

Author Comment

by:JDL129
ID: 39270231
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
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.

 

Author Comment

by:JDL129
ID: 39270244
Where we get the error is when we move through the tags and start the Body tag.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39270247
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
 

Author Comment

by:JDL129
ID: 39270262
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
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 400 total points
ID: 39270300
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39270305
have to go... :).
0
 

Author Comment

by:JDL129
ID: 39271243
anarki_Jimbel!!!!!!!!  Thanks for the post!!

Attached is the sample I am trying to create.

Thanks,

Jerry
RefillRequest.txt
0
 

Author Closing Comment

by:JDL129
ID: 39271891
Hey guys!!!  You are the best!!

anarki_jimbel's last post hit the nail on the head!!!
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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

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

16 Experts available now in Live!

Get 1:1 Help Now