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.
the highlight yellow is what i want to create
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

teknovationAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anil GolamariCommented:
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", ""))));
0
teknovationAuthor Commented:
your example is doing similar to mines, just need help adding that extra node.
0
Fernando SotoRetiredCommented:
It is already wrapped around the Orders tag. Can you please post how you want the final document to look like. please.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

teknovationAuthor Commented:
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
0
teknovationAuthor Commented:
i would want the url piece to the tag too, thanks
0
Anil GolamariCommented:
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

0
Fernando SotoRetiredCommented:
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

0
teknovationAuthor Commented:
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
0
Fernando SotoRetiredCommented:
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.
0
teknovationAuthor Commented:
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
0
Anil GolamariCommented:
Below code worked for me. But there is a little change in the code but I have used xmldocument instead of LINQ.

 XmlDocument doc = new XmlDocument();  
                     
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);
            XmlElement element1 = doc.CreateElement(string.Empty, "Orders", "http://google.com");
            doc.AppendChild(element1);

            XmlElement element2 = doc.CreateElement("Order");
            element1.AppendChild(element2);

                            XmlElement element3 = doc.CreateElement(string.Empty, "Meta", string.Empty);
                            element2.AppendChild(element3);

                            XmlElement element4 = doc.CreateElement(string.Empty, "Headers", string.Empty);
                            element2.AppendChild(element4);

                            XmlElement element5 = doc.CreateElement(string.Empty, "LineItems", string.Empty);
                            element2.AppendChild(element5);

                            XmlElement element6 = doc.CreateElement(string.Empty, "Summary", string.Empty);
                            element2.AppendChild(element6);

Open in new window


<?xml version="1.0" encoding="UTF-8"?>
-<Orders xmlns="http://google.com">
-<Order xmlns="">
<Meta/>
<Headers/>
<LineItems/>
<Summary/>
</Order>
</Orders>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.