Modifying xml

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veenna
    New Member
    • Dec 2007
    • 65

    Modifying xml

    hi all,

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


    Code:
    <?xml version='1.0' encoding='utf-8' ?>
    <Purchase_Info>
    <Purchase_Order_Proposal>
        <Purchase_Order_Proposal id='1'>pop1</Purchase_Order_Proposal>
        <Purchase_Order_Proposal id='2'>pop2</Purchase_Order_Proposal>
        <Purchase_Order_Proposal id='3'>pop3</Purchase_Order_Proposal>
        <Purchase_Order_Proposal id='4'>pop4</Purchase_Order_Proposal>
        <Purchase_Order_Proposal id='5'>pop5</Purchase_Order_Proposal>
        <Purchase_Order_Proposal id='6'>pop6</Purchase_Order_Proposal>
      </Purchase_Order_Proposal>
    
      <Purchase_Order>
        <Purchase_Order id='1'>po1</Purchase_Order>
        <Purchase_Order id='2'>po2</Purchase_Order>
        <Purchase_Order id='3'>po3</Purchase_Order>
        <Purchase_Order id='4'>po4</Purchase_Order>
        <Purchase_Order id='5'>po5</Purchase_Order>
        <Purchase_Order id='6'>po6</Purchase_Order>
      </Purchase_Order>
    </Purchase_Info>

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

    Code:
    string filePath = “C:\\allFields.xml";
    
                System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
                string strFileContent = streamReader.ReadToEnd();
                XDocument xDocument = XDocument.Parse(strFileContent);
    
    var listArray = from g in xDocument.Descendants().Descendants("Purchase_Info")
                                select new
                                {
                                    fields = g.Element("Purchase_Order_Proposal").Value
                                };
    
    
                foreach (var itm in listArray)
                {
                    var fields = itm.fields.Trim();                
                }
    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
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you always overwrite your fields variable in the foreach loop.

    Comment

    • veenna
      New Member
      • Dec 2007
      • 65

      #3
      Originally posted by Dormilich
      you always overwrite your fields variable in the foreach loop.
      hai dormilich,

      Thank you replying..

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

      regards,
      Veena

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I have no idea, since I don’t know that programming language.

        in PHP you could do
        Code:
        foreach ($listArray as $itm) 
        {
            $fields [B].=[/B] trim($itm->fields);
        }

        Comment

        • Monomachus
          Recognized Expert New Member
          • Apr 2008
          • 127

          #5
          Originally posted by veenna
          hai dormilich,

          Thank you replying..

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

          regards,
          Veena
          What are you trying to achieve? Simple get the 6 elements 'Purchase_Order _Proposal' ?

          If so just write this lines.

          Code:
           XDocument xDocument = XDocument.Load("PurchaseInfoXml.xml");
          
                      var purchaseOrderProposals = xDocument.Descendants("Purchase_Order_Proposal");

          Comment

          • veenna
            New Member
            • Dec 2007
            • 65

            #6
            Originally posted by Monomachus
            What are you trying to achieve? Simple get the 6 elements 'Purchase_Order _Proposal' ?

            If so just write this lines.

            Code:
             XDocument xDocument = XDocument.Load("PurchaseInfoXml.xml");
            
                        var purchaseOrderProposals = xDocument.Descendants("Purchase_Order_Proposal");
            thank you Monomachus
            its working fine..

            regards,
            veena

            Comment

            Working...