Answered write XML with XElement

  • Tuesday, March 06, 2012 1:18 PM
     
      Has Code

    Hello guys,

    I want to create XML like this in a loop because I don't know how much data is in my list. I could not find an example, all samples are for pre-defined number of data. 

    So, XML should look like this:

    <AM>
    	<HM>
    		<FullName>aaa</FullName>
    		<ShortName>aa</ShortName>
    		<Code>1</Code>
    	</HM>
    	<HM>
    		<FullName>aaa</FullName>
    		<ShortName>aa</ShortName>
    		<Code>2</Code>
    	</HM>	
    </AM>

    There is only one AM buth a lot of HM's. I store them in a list. 

    I have tried with Exlement.Add but I get an non-closed element like <HM\> and not </HM>.

    Please help.

All Replies

  • Tuesday, March 06, 2012 1:46 PM
     
      Has Code

    Try like this:

    XElement elem = new XElement("AM");
    int i = 1;
    while (i < 5)
    {
        elem.Add(new XElement("HM",
            new XElement("FullName", "aaa"),
            new XElement("ShortName", "aa"),
            new XElement("Code", i)
            ));
        i++;
    }

  • Tuesday, March 06, 2012 3:13 PM
     
     Answered Has Code

    Hi crtlSpace;

    Here is sample code on how can achieve the XML document you are looking for. The sample code has a List of objects of type AMInfo which contains the data to build the XML document.

    // Test Data
    List<AMInfo> amInfo = new List<AMInfo>() {
        new AMInfo() { FullName = "aaa", ShortName = "aa", Code = 1},
        new AMInfo() { FullName = "aaa", ShortName = "aa", Code = 2},
        new AMInfo() { FullName = "bbb", ShortName = "bb", Code = 3},
        new AMInfo() { FullName = "ccc", ShortName = "cc", Code = 4}        
        };
        
    // Query to build XML document    
    var amXML = new XElement("AM",
        from h in amInfo
        select new XElement("HM",
            new XElement ("FullName", h.FullName),
                new XElement ("ShortName", h.ShortName),
                new XElement ("Code", h.Code)));
    
                
    amXML.Save("C:/Working Directory/Am.xml");    
    
    
    // Test data class
    public class AMInfo
    {
        public string FullName { get; set; }
        public string ShortName { get; set; }
        public int Code { get; set; }
    }

    And the results of executing the above code.

    // The document saved to disk
    <?xml version="1.0" encoding="utf-8"?>
    <AM>
      <HM>
        <FullName>aaa</FullName>
        <ShortName>aa</ShortName>
        <Code>1</Code>
      </HM>
      <HM>
        <FullName>aaa</FullName>
        <ShortName>aa</ShortName>
        <Code>2</Code>
      </HM>
      <HM>
        <FullName>bbb</FullName>
        <ShortName>bb</ShortName>
        <Code>3</Code>
      </HM>
      <HM>
        <FullName>ccc</FullName>
        <ShortName>cc</ShortName>
        <Code>4</Code>
      </HM>
    </AM>


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked As Answer by crtlSpace Wednesday, March 07, 2012 8:03 AM
    •