Solved

LINQ Question

Posted on 2011-09-07
9
199 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
 

Author Comment

by:SubbuUSA
ID: 36505966
@apeter

Could you please help me..I would have definitely done if I would have known it
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.

 
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

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

Suggested Solutions

Title # Comments Views Activity
c# if statement weird reaction 3 42
Data is not showing from images 15 37
Angular - break the dataset in chunks? 7 39
C#  Radio button search for Date not DateTime 4 32
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

22 Experts available now in Live!

Get 1:1 Help Now