我需要将XML放入SQL数据库中。我正在使用LINQ to XML从XML中获取我想要的数据。
在我看来,我通过将变量与Linq映射到XML然后保存这些变量而浪费了大量时间。我想知道我是否可以结合这些步骤。
这是一个示例查询。我在类似数据上多次执行此操作和其他操作。
也许以某种方式让LINQ to XML直接创建我的LINQ对象而不是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();
}
}
答案 0 :(得分: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",
…
}
您可能还希望在循环之外而不是在循环中调用提交。