XDocument 到 dataSet,我的嵌套节点被视为新表?
当循环遍历元素时,它认为子节点是一个表,而实际上它应该只是一列。我的大部分代码都运行良好,直到代码到达这部分:(我的所有代码都在底部)
new XElement("TBL_Magic_TripCountries", lstCountry.Items
.Cast<ListItem>()
.Where(i=> i.Selected)
.Select(x => new XElement("CountryCode", x.Value))
),
我收到无法访问目标表“CountryCode”。我不知道为什么它认为这是一个嵌套在另一个节点中的表。我试图从 TBL_Magic_TripCountries 设置 CountryCode,但它没有像我想象的那样工作。有什么帮助吗?
XDocument xmlDoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Created: " + DateTime.Now.ToString()),
new XElement("Trips",
new XElement("TBL_TripDetails",
new XElement("DepartureDate", txtDepartureDate.Text),
new XElement("ReturnDate", txtReturnDate.Text),
new XElement("TripRecommendation", tripRecommend),
new XElement("TripRecommendationComment", txtTripRecommendComment.Text),
new XElement("TripFavouriteThing", txtLikeMostComment.Text)
),
new XElement("TBL_Magic_TripCountries", lstCountry.Items
.Cast<ListItem>()
.Where(i=> i.Selected)
.Select(x => new XElement("CountryCode", x.Value))
),
new XElement("TBL_Cities", lstCities.Items
.Cast<ListItem>()
.Select(x => new XElement("City", x.Text))),)
));
...
DataSet ds = new DataSet();
ds.ReadXml(xDoc.CreateReader());
string StrConn = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
SqlConnection cnn = new SqlConnection(StrConn);
SqlBulkCopy blkcopy = new SqlBulkCopy(cnn);
cnn.Open();
foreach (DataTable dt in ds.Tables)
{
BulkCopyTable(dt, blkcopy);
}
编辑 在我开始将其放入数据集中之前,我的 XML 看起来像这样
<!--Created: 8/4/2010 8:28:10 AM-->
<Trips>
<TBL_TripDetails>
<DepartureDate>2/2/2010</DepartureDate>
<ReturnDate>2/2/2010</ReturnDate>
<TripTypeA>1</TripTypeA>
<TripTypeB>2</TripTypeB>
<PurposeOfTrip>vacation</PurposeOfTrip>
<RegionID>2</RegionID>
<OverallRating>0</OverallRating>
<SupplierID>3</SupplierID>
<SupplierComment>great supplier</SupplierComment>
<FoodRating>0</FoodRating>
<CulinaryComments></CulinaryComments>
<RecommendRestaurant>false</RecommendRestaurant>
<AttractionDisappoint>false</AttractionDisappoint>
<TripRecommendation>false</TripRecommendation>
</TBL_TripDetails>
<TBL_Magic_TripCountries>
<CountryCode>USA</CountryCode>
<CountryCode>CND</CountryCode>
</TBL_Magic_TripCountries>
<TBL_Cities>
<City>New York</City>
<City>Ottawa</City>
<City>Las Vegas</City>
</TBL_Cities>
<TBL_Magic_TripTransportation>
<TransportType>3</TransportType>
<TransportType>4</TransportType>
</TBL_Magic_TripTransportation>
</Trips>
When looping through elements it thinks the child node is a table, when it should just be a column. I have most of the code working well, until I the code gets to this part: (all of my code is at the bottom)
new XElement("TBL_Magic_TripCountries", lstCountry.Items
.Cast<ListItem>()
.Where(i=> i.Selected)
.Select(x => new XElement("CountryCode", x.Value))
),
I am receiving a Cannot access destination table 'CountryCode'. I have no idea why this thinks this is a table if its nested within another node. I am trying to set CountryCode from TBL_Magic_TripCountries, but its not working as I thought. Any help?
XDocument xmlDoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Created: " + DateTime.Now.ToString()),
new XElement("Trips",
new XElement("TBL_TripDetails",
new XElement("DepartureDate", txtDepartureDate.Text),
new XElement("ReturnDate", txtReturnDate.Text),
new XElement("TripRecommendation", tripRecommend),
new XElement("TripRecommendationComment", txtTripRecommendComment.Text),
new XElement("TripFavouriteThing", txtLikeMostComment.Text)
),
new XElement("TBL_Magic_TripCountries", lstCountry.Items
.Cast<ListItem>()
.Where(i=> i.Selected)
.Select(x => new XElement("CountryCode", x.Value))
),
new XElement("TBL_Cities", lstCities.Items
.Cast<ListItem>()
.Select(x => new XElement("City", x.Text))),)
));
...
DataSet ds = new DataSet();
ds.ReadXml(xDoc.CreateReader());
string StrConn = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
SqlConnection cnn = new SqlConnection(StrConn);
SqlBulkCopy blkcopy = new SqlBulkCopy(cnn);
cnn.Open();
foreach (DataTable dt in ds.Tables)
{
BulkCopyTable(dt, blkcopy);
}
EDIT my XML looks like this before I start putting it in the dataset
<!--Created: 8/4/2010 8:28:10 AM-->
<Trips>
<TBL_TripDetails>
<DepartureDate>2/2/2010</DepartureDate>
<ReturnDate>2/2/2010</ReturnDate>
<TripTypeA>1</TripTypeA>
<TripTypeB>2</TripTypeB>
<PurposeOfTrip>vacation</PurposeOfTrip>
<RegionID>2</RegionID>
<OverallRating>0</OverallRating>
<SupplierID>3</SupplierID>
<SupplierComment>great supplier</SupplierComment>
<FoodRating>0</FoodRating>
<CulinaryComments></CulinaryComments>
<RecommendRestaurant>false</RecommendRestaurant>
<AttractionDisappoint>false</AttractionDisappoint>
<TripRecommendation>false</TripRecommendation>
</TBL_TripDetails>
<TBL_Magic_TripCountries>
<CountryCode>USA</CountryCode>
<CountryCode>CND</CountryCode>
</TBL_Magic_TripCountries>
<TBL_Cities>
<City>New York</City>
<City>Ottawa</City>
<City>Las Vegas</City>
</TBL_Cities>
<TBL_Magic_TripTransportation>
<TransportType>3</TransportType>
<TransportType>4</TransportType>
</TBL_Magic_TripTransportation>
</Trips>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能确定这就是原因,但
会重新运行 IEnumerable并将被数据集解释为重数为 0..∞ 的元素。数据集会在内部将该元素转换为表,因为这就是数据集处理嵌套关系的方式。我不确定您最终是否会得到两个表(与主表关联的“TBL_Magic_TripCountries”,然后与其关联的“CountryCode”)或一个以 CountryCode 作为唯一列的表“TBL_Magic_TripCountries”。我已经很久没有处理自动模式生成的数据集了。
I can't be certain this is the cause, but
retruns an IEnumerable<XElement> and would be interpreted by the dataset as an element with multiplicity of 0..∞. Datasets will translate that element as a table internally because that's how datasets handle nested relationships. I'm not sure if you'd end up with two tables ("TBL_Magic_TripCountries" associated with your main table and then "CountryCode" associated with it) or a single table "TBL_Magic_TripCountries" with CountryCode as the only column. It's been too long since I dealt with auto-schema-generated datasets.