locked
child xml nodes RRS feed

  • Question

  • User647458646 posted

    Dear all, 

    With my current code, I am getting output as shown in output1:

    			string autoDate = uriBuilder.activateMethod();
                XElement xeRoot = new XElement("mailjob");
    
                XElement xeSendDate = new XElement("send_datetime", autoDate);
                xeRoot.Add(xeSendDate);
    
                XElement xeStatus = new XElement("subject", "Draft");
                xeRoot.Add(xeStatus);
    
                XElement xeDisplayDate = new XElement("from_name", "MCI");
                xeRoot.Add(xeDisplayDate);
    
                XElement xeFormat = new XElement("format", "HTML");
                xeRoot.Add(xeFormat);
    
                XElement xeSendTo = new XElement("to");
                xeSendTo.Add(new XAttribute("target_type", "###"));
                xeSendTo.Add(new XAttribute("targets", "####"));
                xeRoot.Add(xeSendTo);
    
                XDocument xDoc = new XDocument(xeRoot);
                return xDoc.ToString();

    Ouput1:

    	                <mailjob> 
    			<send_datetime>12-09-2014 15:20:31</send_datetime> 
    			<subject>Draft</subject> 
    			<from_name>MCI</from_name> 
    			<format>HTML</format> <to target_type="####" targets="#####" /> 
    			</mailjob> 

    However, I would create sub child for node<to>, such as shown below:

    <mailjob> 
    			<send_datetime>12-09-2014 15:20:31</send_datetime> 
    			<subject>Draft</subject> 
    			<from_name>MCI</from_name> 
    			<format>HTML</format> 
    			<to>
    			<target_type>####</target_type>
    			<targets>#####</targets>
    			</to>
    			</mailjob> 

    How can I achieve this? Please advice further. Thanks

    Friday, September 12, 2014 12:20 PM

Answers

  • User-328728277 posted

    try with this:

            string autoDate = uriBuilder.activateMethod();
            XElement xeRoot = new XElement("mailjob");
    
            XElement xeSendDate = new XElement("send_datetime", autoDate);
            xeRoot.Add(xeSendDate);
    
            XElement xeStatus = new XElement("subject", "Draft");
            xeRoot.Add(xeStatus);
    
            XElement xeDisplayDate = new XElement("from_name", "MCI");
            xeRoot.Add(xeDisplayDate);
    
            XElement xeFormat = new XElement("format", "HTML");
            xeRoot.Add(xeFormat);
    
            XElement xeSendTo = new XElement("to");
            XElement targettype = new XElement("target_type","###");
            xeSendTo.Add(targettype);
            XElement targets = new XElement("targets", "###");
            xeSendTo.Add(targets);
            //xeSendTo.Add(new XAttribute("target_type", "###"));
            //xeSendTo.Add(new XAttribute("targets", "####"));
            xeRoot.Add(xeSendTo);
    
            XDocument xDoc = new XDocument(xeRoot);
            return xDoc.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 12, 2014 2:01 PM

All replies

  • User-328728277 posted

    try with this:

            string autoDate = uriBuilder.activateMethod();
            XElement xeRoot = new XElement("mailjob");
    
            XElement xeSendDate = new XElement("send_datetime", autoDate);
            xeRoot.Add(xeSendDate);
    
            XElement xeStatus = new XElement("subject", "Draft");
            xeRoot.Add(xeStatus);
    
            XElement xeDisplayDate = new XElement("from_name", "MCI");
            xeRoot.Add(xeDisplayDate);
    
            XElement xeFormat = new XElement("format", "HTML");
            xeRoot.Add(xeFormat);
    
            XElement xeSendTo = new XElement("to");
            XElement targettype = new XElement("target_type","###");
            xeSendTo.Add(targettype);
            XElement targets = new XElement("targets", "###");
            xeSendTo.Add(targets);
            //xeSendTo.Add(new XAttribute("target_type", "###"));
            //xeSendTo.Add(new XAttribute("targets", "####"));
            xeRoot.Add(xeSendTo);
    
            XDocument xDoc = new XDocument(xeRoot);
            return xDoc.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 12, 2014 2:01 PM
  • User647458646 posted

    Many thanks for your help.  :)

    Monday, September 15, 2014 5:51 AM