473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Modifying xml

65
hi all,

I am very new to xml and LINQ.
i have an xml as follows


Expand|Select|Wrap|Line Numbers
  1. <?xml version='1.0' encoding='utf-8' ?>
  2. <Purchase_Info>
  3. <Purchase_Order_Proposal>
  4.     <Purchase_Order_Proposal id='1'>pop1</Purchase_Order_Proposal>
  5.     <Purchase_Order_Proposal id='2'>pop2</Purchase_Order_Proposal>
  6.     <Purchase_Order_Proposal id='3'>pop3</Purchase_Order_Proposal>
  7.     <Purchase_Order_Proposal id='4'>pop4</Purchase_Order_Proposal>
  8.     <Purchase_Order_Proposal id='5'>pop5</Purchase_Order_Proposal>
  9.     <Purchase_Order_Proposal id='6'>pop6</Purchase_Order_Proposal>
  10.   </Purchase_Order_Proposal>
  11.  
  12.   <Purchase_Order>
  13.     <Purchase_Order id='1'>po1</Purchase_Order>
  14.     <Purchase_Order id='2'>po2</Purchase_Order>
  15.     <Purchase_Order id='3'>po3</Purchase_Order>
  16.     <Purchase_Order id='4'>po4</Purchase_Order>
  17.     <Purchase_Order id='5'>po5</Purchase_Order>
  18.     <Purchase_Order id='6'>po6</Purchase_Order>
  19.   </Purchase_Order>
  20. </Purchase_Info>  
  21.  

And i am using linq for reading the xml. Here is my code for reading xml

Expand|Select|Wrap|Line Numbers
  1. string filePath = “C:\\allFields.xml";
  2.  
  3.             System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
  4.             string strFileContent = streamReader.ReadToEnd();
  5.             XDocument xDocument = XDocument.Parse(strFileContent);
  6.  
  7. var listArray = from g in xDocument.Descendants().Descendants("Purchase_Info")
  8.                             select new
  9.                             {
  10.                                 fields = g.Element("Purchase_Order_Proposal").Value
  11.                             };
  12.  
  13.  
  14.             foreach (var itm in listArray)
  15.             {
  16.                 var fields = itm.fields.Trim();                
  17.             }
  18.  
When i am reading the xml i am getting only the first value from the xml (i.e. pop1) but i want the six items in a list.

how to get the six items in a single list. and is that xml valid?

please help..

Regards
Veena
Apr 27 '10 #1

✓ answered by Monomachus

@veenna
What are you trying to achieve? Simple get the 6 elements 'Purchase_Order_Proposal' ?

If so just write this lines.

Expand|Select|Wrap|Line Numbers
  1.  XDocument xDocument = XDocument.Load("PurchaseInfoXml.xml");
  2.  
  3.             var purchaseOrderProposals = xDocument.Descendants("Purchase_Order_Proposal");
  4.  

5 1560
Dormilich
8,658 Expert Mod 8TB
you always overwrite your fields variable in the foreach loop.
Apr 27 '10 #2
veenna
65
@Dormilich
hai dormilich,

Thank you replying..

Instead of over writting how to add it to a list?

regards,
Veena
Apr 27 '10 #3
Dormilich
8,658 Expert Mod 8TB
I have no idea, since I don’t know that programming language.

in PHP you could do
Expand|Select|Wrap|Line Numbers
  1. foreach ($listArray as $itm) 
  2. {
  3.     $fields .= trim($itm->fields);
  4. }
Apr 27 '10 #4
Monomachus
127 Expert 100+
@veenna
What are you trying to achieve? Simple get the 6 elements 'Purchase_Order_Proposal' ?

If so just write this lines.

Expand|Select|Wrap|Line Numbers
  1.  XDocument xDocument = XDocument.Load("PurchaseInfoXml.xml");
  2.  
  3.             var purchaseOrderProposals = xDocument.Descendants("Purchase_Order_Proposal");
  4.  
Apr 27 '10 #5
veenna
65
@Monomachus
thank you Monomachus
its working fine..

regards,
veena
Apr 27 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
15
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB...
2
by: Jean-S?bastien Bolduc | last post by:
I'm afraid this is a silly question, to which I know the answer already. But let me ask anyway... In Python, is there a way to modify a builtin type's methods? for instance, modifying...
6
by: Peter Ballard | last post by:
Whew. I hope that title is descriptive! Hi all, The python tutorial tells me "It is not safe to modify the sequence being iterated over in the loop". But what if my list elements are mutable,...
6
by: qwweeeit | last post by:
Hi all, when I was young I programmed in an interpreted language that allowed to modify itself. Also Python can (writing and running a module, in-line): fNew =open("newModule.py",'w') lNew=...
12
by: pvinodhkumar | last post by:
1) char* p = "Plato"; p = 'r'; // runtime error 2) char c = "Plato"; c = 'i';// ok.Why no runtime here?Why is the contradiction? cout << c << endl;
1
by: A.M-SG | last post by:
Hi, Can I view/modify SOAP message at the client proxy side without using soap extensions? Thank you,
13
by: Robin Becker | last post by:
When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous. Is the following idiom dangerous or unpythonic? def func(a): global func, data data =...
56
by: subramanian100in | last post by:
The standard allows that we can copy strings onto arg, arg, etc. Why is it allowed ? What can be the maximum length of such a string that is copied ?
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.