Solved

LINQ Question

Posted on 2011-09-07
9
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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