Solved

LINQ Question

Posted on 2011-09-07
9
196 Views
Last Modified: 2012-06-22
Dear Experts,

I have a requirement.I need to extract a value and pass it as a string to a method which writes an XML file.

Current method call:

xPurchaseOrder.WriteElementString(PurchaseOrderXML.KIT_LEVEL_INVOICE_ACCOUNT_CODE, XML_NS, kitInfo.kitaccount);

sample Output xml looks like this
   <acctcode>MyNewCode</acctcode>

------------------------

String variable that gets assigned from a DB call (KitInfo.WorkTicket). I would like to parse it one after another and create a statement like above WriteElementString() for each and every xml tags it has like below

------------------
DB call gives me a var char output of this xml file and assigns to Kitinfo.workticket object
<WorkTicket>
<ComponentsList>
<KitSummary KitID="62-4854-PER-DC/FL” Provider =”SRC” />

 <Kit ComponentID="62-1615" QTY="4.00000” Provider =” SRC”/>  
<Kit ComponentID="62-4854" QTY="4.00000" "  Provider =” Vendor1”/>  
  .
  .
 <Kit ComponentID="62-6137-DC/FL" QTY="4.00000" "  Provider =” Vendor2”/>  
 <Kit ComponentID="62-7340" QTY="4.00000" "  Provider =” Vendor3”/>  

</ComponentsList>

< AssemblyInstructions="insert item #'s 62-6137-DC/FL, 62-7340 and 62-1615 into welcome envelope (62-4854) Stack completed kits together and rubber band with TAN rubber band" />

</WorkTicket>

-----------

sorry if it is too much confusing..it is really hard to explain
0
Comment
Question by:SubbuUSA
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
>>> String variable that gets assigned from a DB call (KitInfo.WorkTicket).
>>> DB call gives me a var char output of this xml file and assigns to Kitinfo.workticket object
I'm confused, is it a string variable, var char output, or an object?
What does the KitInfo type look like?
How do you map between the WorkTicket elements and the element names in the PurchaseOrderXML?
0
 

Author Comment

by:SubbuUSA
Comment Utility
@MikeToole

First of all, I appreciate lending your help to me. I know it is little confusing but I will try to my best to explain with the amount of  english proficiency I have :)

My requirement is to generate an XML file.  The below method will write to my xml file generation. This property (KIT_LEVEL_INVOICE_ACCOUNT_CODE) sets my tagname like this <acctcode> and this property (kitInfo.kitaccount) will contain the string data obtained from the DB call. In this case it will have the value as 'MyNewCode'

------------

xPurchaseOrder.WriteElementString(PurchaseOrderXML.KIT_LEVEL_INVOICE_ACCOUNT_CODE, XML_NS, kitInfo.kitaccount);

sample Output xml looks like this    <acctcode>MyNewCode</acctcode>

PurchseOrderXML class will contain

{
KIT_LEVEL_INVOICE_ACCOUNT_CODE = "acctcode";
so on...
}

--------------


Before writing my XML file, My DB call would fetch data from the rowset and populate the KitInfo object. Which will have String, bool, int  declarations.

Public class kitinfo: IDataAccessGetValue
{
int accno{get; set}
string acctcode{get; set}
string workTicket{get; set}
}

When My DB call fills this Data classes, KitInfo object will contain a value which will be typically an XML tags like this below

<WorkTicket><ComponentsList><KitSummary KitID="62-4854-PER-DC/FL” Provider =”SRC” /><Kit ComponentID="62-1615" QTY="4.00000” Provider =” SRC”/><Kit ComponentID="62-4854" QTY="4.00000" "  Provider =” Vendor1”/></ComponentsList>< AssemblyInstructions="insert item #'s 62-6137-DC/FL, 62-7340 and 62-1615 into welcome envelope (62-4854) Stack completed kits together and rubber band with TAN rubber band" /></WorkTicket>

I will assign this to a string variable in my code behind.
String workTicket data = XML Tags get asigned to variable ;

What I need here is:

I would like to parse each and every tags and  assign it like this  <acctcode>MyNewCode</acctcode>

At the same time I do not want to disintegerate the attributes to split from the element tags. here ,
the Qty and Provider should be tied to this component and not as separate tags.
 <Kit ComponentID="62-1615" QTY="4.00000” Provider =” SRC”/>  

I would like my work ticket to fit in the XML generation.  I will not have any problem if it is a single string  variable. Too many XML tags in the string variable is what makes it complex to work.

I hope it helps.
0
 
LVL 25

Expert Comment

by:apeter
Comment Utility
Why can't try using Linq to XML to parse the string ?
0
 

Author Comment

by:SubbuUSA
Comment Utility
@apeter

Could you please help me..I would have definitely done if I would have known it
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
Can you post the detail layout of the output required for the sample workticket?
0
 
LVL 25

Expert Comment

by:apeter
Comment Utility
What is the xml you have and what is the value u want to extract from it ?
0
 

Author Comment

by:SubbuUSA
Comment Utility
@MikeToole and @apeter:

My computer crashed and could not respond today.


I was even thinking of appending option..let me know whether that will work out or not?



Sample XML
----------------
 <acctcode>MyNewCode</acctcode>
 <acctpswd>Password</acctpswd>
 <accno>1234</accno>

work ticket XML from a variable has to be appended with the above statements

<WorkTicket>
<ComponentsList>
<KitSummary KitID="62-4854-PER-DC/FL” Provider =”SRC” />
 <Kit ComponentID="62-1615" QTY="4.00000” Provider =” SRC”/>  
<Kit ComponentID="62-4854" QTY="4.00000" "  Provider =” Vendor1”/>  
  .
  .
 <Kit ComponentID="62-6137-DC/FL" QTY="4.00000" "  Provider =” Vendor2”/>  
 <Kit ComponentID="62-7340" QTY="4.00000" "  Provider =” Vendor3”/>  

</ComponentsList>

< AssemblyInstructions="insert item #'s 62-6137-DC/FL, 62-7340 and 62-1615 into welcome envelope (62-4854) Stack completed kits together and rubber band with TAN rubber band" />

</WorkTicket>
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
Comment Utility
To help you need to define the Target output required.
You've reposted what I understand to be the sample workticket input - which, by the way, contains invalid XML:
< AssemblyInstructions="insert item #'s 62-6137-DC/FL, 62-7340 and 62-1615 into welcome envelope (62-4854) Stack completed kits together and rubber band with TAN rubber band" />

Making some assumptions about what you want, the attached code produces the following output:

<PurchaseOrder>
  <acctcode>MyNewCode</acctcode>
  <acctpswd>Password</acctpswd>
  <accno>1234</accno>
  <WorkTicket>
    <ComponentsList>
      <KitSummary KitID="62-4854-PER-DC/FL" Provider="SRC" />
      <Kit ComponentID="62-1615" QTY="4.00000" Provider="SRC" />
      <Kit ComponentID="62-4854" QTY="4.00000" Provider="Vendor1" />
      <Kit ComponentID="62-6137-DC/FL" QTY="4.00000" Provider="Vendor2" />
      <Kit ComponentID="62-7340" QTY="4.00000" Provider="Vendor3" />
    </ComponentsList>
    <AssemblyInstructions>insert item #'s 62-6137-DC/FL, 62-7340 and 62-1615 into welcome envelope (62-4854) Stack completed kits together and rubber
band with TAN rubber band"
                    </AssemblyInstructions>
  </WorkTicket>
</PurchaseOrder>
public static void Test()
        {
            XElement X = XElement.Parse(@"<PurchaseOrder><acctcode>MyNewCode</acctcode> <acctpswd>Password</acctpswd>  <accno>1234</accno> </PurchaseOrder>");
            var WT = XElement.Parse(
                    @"<WorkTicket>
                        <ComponentsList>
                            <KitSummary KitID=""62-4854-PER-DC/FL"" Provider =""SRC"" /> 
                            <Kit ComponentID=""62-1615"" QTY=""4.00000"" Provider=""SRC""/>  
                            <Kit ComponentID=""62-4854"" QTY=""4.00000"" Provider=""Vendor1""/>  
                            <Kit ComponentID=""62-6137-DC/FL"" QTY=""4.00000"" Provider=""Vendor2""/>   
                            <Kit ComponentID=""62-7340"" QTY=""4.00000"" Provider=""Vendor3""/>  
                        </ComponentsList>
                    <AssemblyInstructions>insert item #'s 62-6137-DC/FL, 62-7340 and 62-1615 into welcome envelope (62-4854) Stack completed kits together and rubber band with TAN rubber band""
                    </AssemblyInstructions>
                    </WorkTicket>");

            X.Add(WT);
            Console.Write(X.ToString());
        }

Open in new window

0
 

Author Closing Comment

by:SubbuUSA
Comment Utility
Thanks a lot for your help
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

9 Experts available now in Live!

Get 1:1 Help Now