Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:46:21+00:00 2026-05-27T09:46:21+00:00

I suspect this answer is in SO but I cannot find it. Some reports

  • 0

I suspect this answer is in SO but I cannot find it. Some reports I generate from SQL 2008 R2 in a WPF application I would like to export to XML. The most common use of the XML is to import into Excel. Using TSQL and for xml auto I was able to to generate some rows and then manually edit in the header and root to get a valid XML document. My question is how do I generate a valid XML file via C# .NET 4.0 WPF. Do I just query using FOR XML AUTO then use XmlReader to iterate through the rows and write to file? How do I get a valid first line, root, and closing tags?

From what have read for forward only processing XMLreader and XMLwriter are faster than LINQ XML. XMLwriter also has the option of writing directly to disk as I may need to write out up to 1,000,000 lines of XML. My thought for now is to read the data with a SQLdataReader and write with XMLwriter. Does anyone think there is a faster way?

Code posted for John Sanders as he down voted me for giving no effort. John later removed the down vote and provided the accepted answer to my question.

        XDocument xDoc = new XDocument(
            new XDeclaration("1.0","utf-8", "yes") 
                , new XComment("Gabriel Extract")
                //, new XElement("Documents", new XElement("sDoc", new XAttribute("sID", "1")),
                //    new XElement("sDoc", new XAttribute("sID", "1")
                    //)
                //)
        );
        Debug.WriteLine(xDoc);
        XElement xElementDocs = new XElement("Documents"); 
        XElement xElementsDoc;
        XElement xElementAdd;
        using (SqlConnection connection = new SqlConnection(connString))
        {
            connection.Open();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "select top 100 sID, sParID, docID, attBeg " + Environment.NewLine +
                ",[addDate],[mimeType],[caseID],[textSize],[textHash],[nativeFileName],[nativeFileSize]" + Environment.NewLine +
                ",[nativeMD5],[nativeUNC],[nativeDateCreate],[nativeDateModify],[nativeExtension]" + Environment.NewLine +
                "from docSVsys with (nolock)";
            SqlDataReader rdr = command.ExecuteReader();
            while (rdr.Read())
            {
                //Debug.WriteLine(rdr.GetInt32(0).ToString());
                xElementsDoc = new XElement("sDoc", new XAttribute("sParID", rdr.GetInt32(1).ToString()), new XAttribute("sID", rdr.GetInt32(0).ToString()));
                xElementsDoc.Add(new XElement("docID", rdr.GetString(2)));
                xElementsDoc.Add(new XElement("attBeg", rdr.GetString(3)));
                xElementDocs.Add(xElementsDoc);
            }
            rdr.Close();
        }
        xElementsDoc = (from xml2 in xElementDocs.Descendants("sDoc")
                        where xml2.Attribute("sID").Value == "2"
                        select xml2).FirstOrDefault();
        Debug.WriteLine(xElementsDoc);
        xElementsDoc.Add(new XElement("SVtext", "value"));
        xElementAdd = new XElement("MVtext1", "value1;value2");
        //xElement.Add(new XElement("value", "value1"));
        //xElement.Add(new XElement("value", "value2"));
        xElementsDoc.Add(xElementAdd);
        xElementsDoc = (from xml2 in xElementDocs.Descendants("sDoc")
                        where xml2.Attribute("sID").Value == "4"
                        select xml2).FirstOrDefault();
        Debug.WriteLine(xElementsDoc);
        xElementsDoc.Add(new XElement("SVtext", "value4"));
        xElementAdd = new XElement("MVtext1", "value41;value42");
        //xElement.Add(new XElement("value", "value1"));
        //xElement.Add(new XElement("value", "value2"));
        xElementsDoc.Add(xElementAdd);
        xDoc.Add(xElementDocs);
        //Debug.WriteLine(xDoc);
        xDoc.Save(@"C:\temp\xDocGabe.xml");

Will also build and XMLwriter version and compare performance. If anyone expresses and interest I will share my findings.

What I am finding is this problem is bigger than a bread box. There are 5 fk tables for multivalue fields that I need to get data from. The idea was to perform 6 queries then read through them using rdr.nextresult set. From a SQL perspective an efficient approach. What this approach requires is find the element using where xml2.Attribute(“sID”).Value == “X”. At 1000 records it is done in seconds. At 10,000 is take minutes and I need it to scale to 100,000. The other problem I have is the I need the multivalue to appear as a single value with the value concatenated and separated with a ;. So I am either going to need to write SQL that flattens those columns or write and XML transform and I don’t know how to do either. Or I may read those fk table results into a DictionaryList and use XMLwriter (sounds grunge but DictionaryList is fast). For now I am putting the feature out with 1000 max and Xdocument based. LINQ is convenient but it is not always fast.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:46:22+00:00Added an answer on May 27, 2026 at 9:46 am

    I believe you may be looking for the ROOT clause:

    SELECT whatever
    FROM wherever
    FOR XML AUTO, ROOT('rootElementName')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suspect there is an easy answer for this question but I can't find
I suspect there's probably an easy answer to this I'm just not seeing, but
I would suspect this is a simple question but I am not able to
I suspect this applies to general ASP.Net too but I am not sure. If
Just as in title. Is suspect it is, but I couldn't find it anywhere
I'll eat my hat if I get a good answer to this...I suspect that
EDIT: I suspect this is resulting from a bug in my HTC sense UI.
I suspect this is a common problem, but I counldn't seem to locate the
I suspect the answer to this is 'no' (as with so many iPhone questions),
I think I know the answer to this one, but I have just spotted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.