将来自XML的数据存储在关系SQL数据库中
我需要将 XML 文件中的数据存储到带有 2 个表的 SQL 数据库中。 XML 文件如下所示(完整文件有更多 Document
节点):
<Documents>
<Document>
<Number>110</Number>
<Date>2020-10-23</Date>
<TotalPrice>3800</TotalPrice>
<Items>
<Item>
<SKU>001234</SKU>
<Name>FirstItem</Name>
<Quantity>10</Quantity>
<Price>1550</Price>
</Item>
<Item>
<SKU>001235</SKU>
<Name>SecondItem</Name>
<Quantity>8</Quantity>
<Price>1200</Price>
</Item>
<Item>
<SKU>001236</SKU>
<Name>ThirdItem</Name>
<Quantity>21</Quantity>
<Price>1050</Price>
</Item>
</Items>
</Document>
</Documents>
SQL 数据库有 2 个表。一份用于文档,另一份用于项目。
create table Document(
DocumentId int identity(1,1) not null primary key,
Number int not null,
[Date] date not null,
TotalPrice money not null
);
create table Item(
ItemId int identity(1,1) not null primary key,
SKU int not null,
[Name] nvarchar(30) not null,
Quantity int not null,
Price money not null,
DocumentId int not null foreign key references Document(DocumentId)
);
以前,我只将简单的 XML 文件存储到只有一张表的 SQL 数据库中。我这样做的方式(假设我们只有文档表,我们可以忽略项目):
DocumentMetadata.cs
[Serializable]
[XmlRoot("Document")]
public class DocumentMetadata
{
[XmlElement("Number")]
public int Number { get; set; }
[XmlElement("Date")]
public DateTime Date { get; set; }
[XmlElement("TotalPrice")]
public int TotalPrice { get; set; }
}
[MetadataType(typeof(DocumentMetadata))]
public partial class Document
{
}
Example.cs
XDocument xDoc = XDocument.Load(XmlFile);
List<Document> documentList = xDoc.Descendants("Document").Select(document => new Document
{
Number = Convert.ToInt32(document.Element("Number").Value),
Date = Convert.ToDateTime(document.Element("Date").Value),
TotalPrice = Convert.ToInt32(document.Element("TotalPrice").Value),
}).ToList();
using (DocumentsEntities entity = new DocumentsEntities())
{
foreach (var doc in documentList)
{
var dbDoc = entity.Documents.Where(x => x.Number.Equals(d.Number)).FirstOrDefault();
if (dbDoc != null)
{
dbDoc.Number = doc.Number;
dbDoc.Date = doc.Date;
dbDoc.TotalPrice = doc.TotalPrice;
}
else
{
entity.Documents.Add(doc);
}
}
entity.SaveChanges();
}
因为我现在有一些更复杂的 XML 可以使用,还有 2 个相关的数据库表,我的头到处都是。在这种情况下最好的方法是什么?你能指出我正确的方向吗?提前致谢。
I need to store data from an XML file into SQL database with 2 tables. The XML file looks like this (the full file has more Document
nodes):
<Documents>
<Document>
<Number>110</Number>
<Date>2020-10-23</Date>
<TotalPrice>3800</TotalPrice>
<Items>
<Item>
<SKU>001234</SKU>
<Name>FirstItem</Name>
<Quantity>10</Quantity>
<Price>1550</Price>
</Item>
<Item>
<SKU>001235</SKU>
<Name>SecondItem</Name>
<Quantity>8</Quantity>
<Price>1200</Price>
</Item>
<Item>
<SKU>001236</SKU>
<Name>ThirdItem</Name>
<Quantity>21</Quantity>
<Price>1050</Price>
</Item>
</Items>
</Document>
</Documents>
The SQL database has 2 tables. One for Documents, and the other one for Items.
create table Document(
DocumentId int identity(1,1) not null primary key,
Number int not null,
[Date] date not null,
TotalPrice money not null
);
create table Item(
ItemId int identity(1,1) not null primary key,
SKU int not null,
[Name] nvarchar(30) not null,
Quantity int not null,
Price money not null,
DocumentId int not null foreign key references Document(DocumentId)
);
Previously, I've been storing only simple XML files into SQL databases with only one table. The way that I was doing it (let's say that we just have Document table, and we can ignore Items):
DocumentMetadata.cs
[Serializable]
[XmlRoot("Document")]
public class DocumentMetadata
{
[XmlElement("Number")]
public int Number { get; set; }
[XmlElement("Date")]
public DateTime Date { get; set; }
[XmlElement("TotalPrice")]
public int TotalPrice { get; set; }
}
[MetadataType(typeof(DocumentMetadata))]
public partial class Document
{
}
Example.cs
XDocument xDoc = XDocument.Load(XmlFile);
List<Document> documentList = xDoc.Descendants("Document").Select(document => new Document
{
Number = Convert.ToInt32(document.Element("Number").Value),
Date = Convert.ToDateTime(document.Element("Date").Value),
TotalPrice = Convert.ToInt32(document.Element("TotalPrice").Value),
}).ToList();
using (DocumentsEntities entity = new DocumentsEntities())
{
foreach (var doc in documentList)
{
var dbDoc = entity.Documents.Where(x => x.Number.Equals(d.Number)).FirstOrDefault();
if (dbDoc != null)
{
dbDoc.Number = doc.Number;
dbDoc.Date = doc.Date;
dbDoc.TotalPrice = doc.TotalPrice;
}
else
{
entity.Documents.Add(doc);
}
}
entity.SaveChanges();
}
Since I have a bit more complex XML now to work with, and 2 related database tables, my head is all over the place. What would be the best approach in this situation? Could you point me in the right direction? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不到使用复杂的C#代码切碎XML的要点。 SQL Server可以非常整齐地执行此操作,
您的C#代码可能是类似的,
如果您需要
IDENTITY
ID号,可以使用outpation> outpation
条款entity.fromsqlquery
I don't see the point in shredding the XML using complex C# code. SQL Server can do this pretty neatly
Your C# code could be something like
If you need the
IDENTITY
ID numbers you can use anOUTPUT
clause withentity.FromSqlQuery