0

SQL データベースに入れる必要がある XML があります。LINQ to XML を使用して、必要なデータを XML から取得しています。

Linq を使用して変数を XML にマッピングし、それらの変数を保存することで、多くの時間を無駄にしているように思えます。これらの手順を組み合わせることができるかどうか疑問に思っています。

これはクエリの例です。これを何度も実行し、同様のデータで他のものを実行します。

おそらく、LINQ to XML が独自のオブジェクトを背後で作成するのではなく、LINQ to XML が直接 LINQ オブジェクトを作成するようにします。

これが私のコードです。

        HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(newURI);
        request2.AllowAutoRedirect = true;
        request2.PreAuthenticate = true;
        request2.Credentials = credentialCache;
        request2.AutomaticDecompression = DecompressionMethods.GZip;
        request2.Method = "GET";

        HttpWebResponse response2 = null;
        try
        {
            response2 = (HttpWebResponse)request2.GetResponse();
            HttpStatusCode statusCode2 = response2.StatusCode;

            using (Stream responseData2 = response2.GetResponseStream())
            {
                // TODO: Handle HTTP status code and response data here

                XDocument userInfo = XDocument.Load(responseData2);
                XNamespace userInfoNamespace = "http://www.digitalmeasures.com/schema/data";
                XNamespace dmdNamespace = "http://www.digitalmeasures.com/schema/data-metadata";

                var Records = from Record in userInfo.Descendants(userInfoNamespace + "Record")
                              select new
                              {
                                  college = Record.Element(dmdNamespace + "IndexEntry").Attribute("entryKey").Value,
                                  department = Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().IsEmpty ? Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().Attribute("entryKey").Value : "blank",
                                  prefix = (string)Record.Descendants(userInfoNamespace + "PREFIX").SingleOrDefault(),
                                  fname = (string)Record.Descendants(userInfoNamespace + "FNAME").SingleOrDefault(),
                                  pfname = (string)Record.Descendants(userInfoNamespace + "PFNAME").SingleOrDefault(),
                                  mname = (string)Record.Descendants(userInfoNamespace + "MNAME").SingleOrDefault(),
                                  lname = (string)Record.Descendants(userInfoNamespace + "LNAME").SingleOrDefault(),
                                  suffix = (string)Record.Descendants(userInfoNamespace + "SUFFIX").SingleOrDefault(),
                                  alt_name = (string)Record.Descendants(userInfoNamespace + "ALT_NAME").SingleOrDefault(),
                                  endpos = (string)Record.Descendants(userInfoNamespace + "ENDPOS").SingleOrDefault(),
                                  email = (string)Record.Descendants(userInfoNamespace + "EMAIL").SingleOrDefault(),
                                  building = (string)Record.Descendants(userInfoNamespace + "BUILDING").SingleOrDefault(),
                                  ophone1 = (string)Record.Descendants(userInfoNamespace + "OPHONE1").SingleOrDefault(),
                                  ophone2 = (string)Record.Descendants(userInfoNamespace + "OPHONE2").SingleOrDefault(),
                                  ophone3 = (string)Record.Descendants(userInfoNamespace + "OPHONE3").SingleOrDefault(),
                                  dphone1 = (string)Record.Descendants(userInfoNamespace + "DPHONE1").SingleOrDefault(),
                                  dphone2 = (string)Record.Descendants(userInfoNamespace + "DPHONE2").SingleOrDefault(),
                                  dphone3 = (string)Record.Descendants(userInfoNamespace + "DPHONE3").SingleOrDefault(),
                                  fax1 = (string)Record.Descendants(userInfoNamespace + "FAX1").SingleOrDefault(),
                                  fax2 = (string)Record.Descendants(userInfoNamespace + "FAX2").SingleOrDefault(),
                                  fax3 = (string)Record.Descendants(userInfoNamespace + "FAX3").SingleOrDefault(),
                                  website = (string)Record.Descendants(userInfoNamespace + "WEBSITE").SingleOrDefault(),
                                  gender = (string)Record.Descendants(userInfoNamespace + "GENDER").SingleOrDefault(),
                                  ethnicity = (string)Record.Descendants(userInfoNamespace + "ETHNICITY").SingleOrDefault(),
                                  citizen = (string)Record.Descendants(userInfoNamespace + "CITIZEN").SingleOrDefault(),
                                  bio = (string)Record.Descendants(userInfoNamespace + "BIO").SingleOrDefault(),
                                  teaching_interests = (string)Record.Descendants(userInfoNamespace + "TEACHING_INTERESTS").SingleOrDefault(),
                                  research_interests = (string)Record.Descendants(userInfoNamespace + "RESEARCHINTERETS").SingleOrDefault(),
                                  expertise = (string)Record.Descendants(userInfoNamespace + "EXPERTISE").SingleOrDefault(),
                                  upload_photo = (string)Record.Descendants(userInfoNamespace + "UPLOAD_PHOTO").SingleOrDefault(),
                                  upload_cv = (string)Record.Descendants(userInfoNamespace + "UPLOAD_CV").SingleOrDefault(),
                              };

                foreach (var recordItem in Records)
                {
                    PCI a = new PCI();
                    a.COLLEGE = recordItem.college;
                    a.DEPARTMENT = recordItem.department;
                    a.PREFIX = recordItem.prefix;
                    a.FNAME = recordItem.fname;
                    a.PFNAME = recordItem.pfname;
                    a.MNAME = recordItem.mname;
                    a.LNAME = recordItem.lname;
                    a.SUFFIX = recordItem.suffix;
                    a.ALT_NAME = recordItem.alt_name;
                    a.ENDPOS = recordItem.endpos;
                    a.EMAIL = recordItem.email;
                    a.BUILDING = recordItem.building;
                    a.OPHONE1 = recordItem.ophone1;
                    a.OPHONE2 = recordItem.ophone2;
                    a.OPHONE3 = recordItem.ophone3;
                    a.DPHONE1 = recordItem.dphone1;
                    a.DPHONE2 = recordItem.dphone2;
                    a.DPHONE3 = recordItem.dphone3;
                    a.FAX1 = recordItem.fax1;
                    a.FAX2 = recordItem.fax2;
                    a.FAX3 = recordItem.fax3;
                    a.WEBSITE = recordItem.website;
                    a.GENDER = recordItem.gender;
                    a.ETHNICITY = recordItem.citizen;
                    a.CITIZEN = recordItem.citizen;
                    a.BIO = recordItem.bio;
                    a.TEACHING_INTERESTS = recordItem.teaching_interests;
                    a.RESEARCH_INTERESTS = recordItem.research_interests;
                    a.EXPERTISE = recordItem.expertise;
                    a.UPLOAD_PHOTO = recordItem.upload_photo;
                    a.UPLOAD_CV = recordItem.upload_cv;

                    faimdc.PCIs.InsertOnSubmit(a);
                    faimdc.SubmitChanges();
                }
          }
4

1 に答える 1

0

匿名型に投影する代わりに、PCI クラスを直接作成でき、すべてをコピーする必要はありません。何かのようなもの:

var Records = from Record in userInfo.Descendants(userInfoNamespace + "Record")
              select new PCI
              {
                  COLLEGE = Record.Element(dmdNamespace + "IndexEntry").Attribute("entryKey").Value,
                  DEPARTMENT =  Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().IsEmpty ? Record.Element(dmdNamespace + "IndexEntry").ElementsAfterSelf(dmdNamespace + "IndexEntry").FirstOrDefault().Attribute("entryKey").Value : "blank",
                  …
              }

また、サブミットをループ内ではなくループ外で 1 回呼び出す必要がある場合もあります。

于 2012-05-04T18:06:23.340 に答える