I have an XML file that I'm trying to parse and save to a database in a C# program. For most of the elements in this file, I have been able to use SqlBulkCopy because these elements are arranged well with either unique names for child tags or attributes on the root node. However, I have one element that has child elements with repeating tag names (just "tag") but uses attribute names to describe what it is. I have not been able to save this with SqlBulkCopy, which I would prefer since this file can be as large as 500MB and the SqlBulkCopy class is much faster. I tried the code below, but I can see by debugging that the ds.Tables collection is separating hostproperties and tag. I'm guessing this is just how the ReadXml method works. What would be the easiest way that I could get these tags into a datatable object that has the individual attributes as columns so that I could use SqlBulkCopy?
Current C# Code
DataSet ds = new DataSet();
ds.ReadXml(file.InputStream);
DataTable hostItems = ds.Tables["host"];
conn.Open();
using (SqlBulkCopy sb = new SqlBulkCopy(conn))
{
sb.DestinationTableName = "HOSTS";
sb.ColumnMappings.Add("host-ip", "HOST_IP");
sb.ColumnMappings.Add("host-name", "NAME");
sb.ColumnMappings.Add("system-type", "SSH_FINGERPRINT");
sb.ColumnMappings.Add("os", "OS");
sb.WriteToServer(hostItems);
}
XML File
<host>
<tag name="host-ip">192.168.200.8</tag>
<tag name="host-name">someserver.mydomain.com</tag>
<tag name="system-type">webserver</tag>
<tag name="os">WindowsServer2019</tag>
</host>
...
<host>
<tag name="host-ip">192.168.200.9</tag>
<tag name="host-name">someserver2.mydomain.com</tag>
<tag name="system-type">webserver</tag>
<tag name="os">WindowsServer2019</tag>
<tag name="attributeFirstOneDidntHave">Some nonsense</tag>
</host>
Edit
I failed to mention that not all of the hosts have the same amount of tags. I have updated the XML example to illustrate this.
hostand a table fortag. Host is seen as an 'entity' with related tag 'entities'.