How to create an xml file from linq in c# with multiple nodes - LINQ TO XML IN C# - Complex LINQ TO COMPLEX XML - Free Source Code

LINQ to XML Developers

LINQ to XML targets a variety of developers. For an average developer who just wants to get something done, LINQ to XML makes XML easier by providing a query experience that is similar to SQL. With just a bit of study, programmers can learn to write succinct and powerful queries in their programming language of choice.
Professional developers can use LINQ to XML to greatly increase their productivity. With LINQ to XML, they can write less code that is more expressive, more compact, and more powerful. They can use query expressions from multiple data domains at the same time.
What Is LINQ to XML?LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the .NET Framework programming languages.
LINQ to XML is like the Document Object Model (DOM) in that it brings the XML document into memory. You can query and modify the document, and after you modify it you can save it to a file or serialize it and send it over the Internet. However, LINQ to XML differs from DOM: It provides a new object model that is lighter weight and easier to work with, and that takes advantage of language improvements in Visual C# 2008.
The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes. The query capability of LINQ to XML is comparable in functionality (although not in syntax) to XPath and XQuery. The integration of LINQ in Visual C# 2008 provides stronger typing, compile-time checking, and improved debugger support.
Another advantage of LINQ to XML is the ability to use query results as parameters to XElement and XAttribute object constructors enables a powerful approach to creating XML trees. This approach, called functional construction, enables developers to easily transform XML trees from one shape to another.
For example, you might have a typical XML purchase order as described in Sample XML File: Typical Purchase Order (LINQ to XML). By using LINQ to XML, you could run the following query to obtain the part number attribute value for every item element in the purchase order:
Here is the source code:

  SmartRexEntities db = new SmartRexEntities();
        string XMLfilePath = Server.MapPath("~/") + "/Documents/"+SessionHelper.LOGEDID_CUSTOMER_ENTITY.member.First_Name+"_"+DateTime.Now+".xml";
        using (FileStream sr = File.Create(XMLfilePath))
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.OmitXmlDeclaration = true;
            xws.Indent = true;
            xws.Encoding = new System.Text.UTF8Encoding();

            using (XmlWriter xw = XmlWriter.Create(sr, xws))
            {
                //xw.WriteNode(xmlStartingTag, true);
                //xw.WriteStartDocument();
                var els = from c in db.viewSubCategories
                          orderby c.Parent_ID
                          select c;
                var xml = new XElement("Categories",
                els.ToList()
                .Select(e => new XElement("category",
                            new XAttribute("name", e.Name))));
                //xml.Save(xw);
                //xw.WriteEndDocument();
            }
            using (XmlWriter xw2 = XmlWriter.Create(sr, xws))
            {
                var xml = new XElement("Data");
                var els = from c in db.tblPatternCategories
                          orderby c.Name
                          select c;
                var xmlCategory = new XElement("categories",
                els.ToList()
                .Select(e => new XElement("category",
                             new XAttribute("name", e.Name))));

                var Colors = from d in db.tblColors
                             select new { d.name, d.value };
                var xmlColors = new XElement("colors",
                Colors.ToList()
                .Select(e => new XElement("item",
                             new XElement("name", e.name),
                             new XElement("value", e.value))));


                var patt = from o in db.viewPattern
                           select new { o.category, o.Name, o.ID, o.ImageURL, o.GrayScaleColors, o.Pattern_Repeat, o.Quality, o.Colors, o.exampleColors };
                var xmlPatterns = new XElement("patterns",
                patt.ToList()
                .Select(e => new XElement("item",
                             new XElement("category", e.category),
                             new XElement("name", e.Name),
                             new XElement("id", e.ID),
                             new XElement("url", e.ImageURL),
                             new XElement("repeat", e.Pattern_Repeat),
                             new XElement("quality", e.Quality),
                             new XElement("greyscale", getTrimmedSemiColonString(e.GrayScaleColors.Replace(";;", ""))),
                             new XElement("example1", getTrimmedSemiColonString(e.Colors.Replace(";;", ""))),
                             new XElement("example2", getTrimmedSemiColonString(e.exampleColors.Replace(";;", ""))))));
                xml.Add(xmlCategory);
                xml.Add(xmlColors);
                xml.Add(xmlPatterns);
                xml.Save(xw2);


Post a Comment

0 Comments