English 中文(简体)
锡ML数据组(多组)
原标题:Linq Group (multiple) on XML Data

我正试图将一项统一的XML数据归为等级夹。

如下文所示代码。 我正试图找到以下办法:

  1. How can I change the synatx for Console.WriteLine(" {0}, f.Element("FILE_NAME").Value); to something like f.FileName in the inner foreach statement.
  2. Is there anything else I can do to make it more efficient and/or readable?

我目前正在4年左右,但想想到的是,新版本的新特点使这种信条变得更形。

成就

法典:

using System;
using System.Linq;
using System.Xml.Linq;

namespace TestingStuff
{
    class LinqQuestion
    {
        static void Main(string[] args)
        {
            Question();

            #region End Console
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("*".PadRight(30,  * ));
            Console.WriteLine("Done");

#if DEBUG
            {
                Console.WriteLine("Press any key to exit.");
                Console.WriteLine("Where is the any key?  --Homer Simpson");
                Console.ReadKey(true);
            }
#endif
            #endregion
        }

        private static void Question()
        {
            int count = 0;
            XDocument xmlDoc = XDocument.Parse(
                @"<ROWSET>
                    <ROW>
                      <PARENT_DIR>Parent_100</PARENT_DIR>
                      <DIR>Folder_110</DIR>
                      <FILE_NAME>File_111</FILE_NAME>
                    </ROW>
                    <ROW>
                      <PARENT_DIR>Parent_100</PARENT_DIR>
                      <DIR>Folder_110</DIR>
                      <FILE_NAME>File_112</FILE_NAME>
                    </ROW>
                    <ROW>
                      <PARENT_DIR>Parent_200</PARENT_DIR>
                      <DIR>Folder_210</DIR>
                      <FILE_NAME>File_211</FILE_NAME>
                    </ROW>
                    <ROW>
                      <PARENT_DIR>Parent_200</PARENT_DIR>
                      <DIR>Folder_220</DIR>
                      <FILE_NAME>File_221</FILE_NAME>
                    </ROW>
                  </ROWSET>");


            var rows = from d in xmlDoc.Descendants("ROW")
                       group d by new
                       {   
                           ParentDir = d.Element("PARENT_DIR").Value,
                           Dir = d.Element("DIR").Value
                       }
                       ;
            foreach (var myRow in rows)
            {

                /* 
                 * Create Folders
                 * Folder: PARENT_DIRDIRFILE_NAME
                 * 
                 */
                try
                {

                    string[] folders = new string[] { myRow.Key.ParentDir, myRow.Key.Dir };
                    string newFolder = String.Join("\", folders);

                    count++;

                    Console.WriteLine("{0}	{1}", count, newFolder);

                    foreach (var f in myRow)
                    {
                        Console.WriteLine("		{0}", f.Element("FILE_NAME").Value);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error in Copy Process:");
                    // Specify the XML details for failed file / row
                    Console.WriteLine(ex.Message);
                }

            }
            Console.WriteLine();
            Console.WriteLine("Total Count: {0}", count);
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}
最佳回答
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签