Solved

LINQ Question

Posted on 2011-09-07
9
202 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
ID: 36501535
>>> 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
ID: 36502211
@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
ID: 36505871
Why can't try using Linq to XML to parse the string ?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:SubbuUSA
ID: 36505966
@apeter

Could you please help me..I would have definitely done if I would have known it
0
 
LVL 27

Expert Comment

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

Expert Comment

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

Author Comment

by:SubbuUSA
ID: 36514358
@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
ID: 36518764
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
ID: 36523246
Thanks a lot for your help
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

786 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