Link to home
Start Free TrialLog in
Avatar of teknovation
teknovation

asked on

C# XML Nodes

Hi All,

I need help creating this node in the xml file, can someone assist?

Here's what I have so far, which works and looks great, but I need it all wrapped around the Orders tag.
User generated image
Here's my current code:
 
XmlDocument xml = new XmlDocument();
            XmlDeclaration xmlDeclaration = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = xml.CreateElement("Order");
            xml.InsertBefore(xmlDeclaration, xml.DocumentElement);
            xml.AppendChild(root);
 
                    XmlElement child_meta_table = xml.CreateElement("Meta");
                    root.AppendChild(child_meta_table);

XmlElement child_meta_table = xml.CreateElement("Header");
                    root.AppendChild(child_header_table);

XmlElement child_meta_table = xml.CreateElement("LineItems");
                    root.AppendChild(child_lineitems_table);

XmlElement child_meta_table = xml.CreateElement("Summary");
                    root.AppendChild(child_Summary_table);

Open in new window

Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

Hi,

You can use XDocument to create an xml file and below is sample code that should be able to create an xml file.        

   var doc = new XDocument(new XElement("Order",
                             new XElement("Meta",
                                 new XAttribute("name", ""),
                                 new XAttribute("value", "")),
                             new XElement("Headers",
                                 new XAttribute("name", ""),
                                 new XAttribute("value", "")),
                             new XElement("LineItem",
                                 new XAttribute("name", ""),
                                 new XAttribute("value", "")),
                             new XElement("Summary",
                                 new XAttribute("name", ""),
                                 new XAttribute("value", ""))));
Avatar of teknovation
teknovation

ASKER

your example is doing similar to mines, just need help adding that extra node.
It is already wrapped around the Orders tag. Can you please post how you want the final document to look like. please.
the photo embedded is what i want.

my code only has it wrapped around the Order tag and not Orders. Thats why its highlighted yellow
i would want the url piece to the tag too, thanks
Sorry that's my bad I am confused by both order and orders.

 var doc = new XDocument(new XElement("Orders",
                                        new XAttribute("xmlns",""),                               
                                            new XElement("Order",
                                                         new XElement("Meta",
                                                             new XAttribute("name", ""),
                                                             new XAttribute("value", "")),
                                                         new XElement("Headers",
                                                             new XAttribute("name", ""),
                                                             new XAttribute("value", "")),
                                                         new XElement("LineItem",
                                                             new XAttribute("name", ""),
                                                             new XAttribute("value", "")),
                                                         new XElement("Summary",
                                                             new XAttribute("name", ""),
                                                             new XAttribute("value", "")))));

Open in new window


OutPut
<?xml version="1.0"?>
-<Orders xmlns="">
-<Order>
<Meta value="" name=""/>
<Headers value="" name=""/>
<LineItem value="" name=""/>
<Summary value="" name=""/>
</Order>
</Orders>

Open in new window

Hi teknovation;

The following code snippet using Linq to XML should give you what you are looking for.
XNamespace ns = "http://www....com";
XDocument xml = new XDocument(new XDeclaration("1.0", "UTF-8", null), 
                    new XElement(ns + "Orders",
                        new XElement(ns + "Order",
                            new XElement(ns + "Meta"),
                            new XElement(ns + "Header"),
                            new XElement(ns + "LineItems"),
                            new XElement(ns + "Summary")
                        )
                    )
                );

Open in new window

thanks but i need to use the code i have? its in c# and not using linq. im updating old code at work and not rewriting a brand new program
Sorry but I never took the time to properly learn XML using the older technology of using XmlDocument. Maybe lucky85 has a better understanding of that technology.
thanks for the reply, hopefully someone does! :)

What makes it more complicated is this is used for extracting data from CRM Dynamic which uses FetchXML
ASKER CERTIFIED SOLUTION
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial