Xml与节点一起注释 [英] Xml comments alongside nodes
问题描述
嗨专家,
拥有以下XML文件样本:
Hi Experts,
Having the following XML file sample:
<App action="A" id="1">
<BaseVeh id="123" /><!-- comment -->
<Qty>1</Qty>
<PartType id="123" /><!-- comment -->
<Part>part name</Part>
</App>
我想读取整个节点/注释并保存到SQL表。
我设法读取所有节点,保存它们没问题,也设法读取所有注释并将它们保存在单独的表中。
我想将注释保存在相应的节点旁边,换句话说,如果我正在保存 partTypeId 我想在同一张桌子上保存它的评论,是否可以这样做?
我尝试了什么:
以下是我如何阅读XML并保存:
I want to read the whole nodes/comments and save to SQL table.
I managed to read all the nodes, save them no problem, also managed to read all comments and save them in separate table too.
I want to save the comment alongside the corresponding node, in other words, if i'm saving partTypeId i want to save it's comment too in the same table, is it possible to do so?
What I have tried:
Here's how I read the XML and save:
XmlDocument mydoc = new XmlDocument();
mydoc.Load("https://abc/file.xml");
XmlElement element = mydoc.DocumentElement;
XmlNodeList nodes = element.SelectNodes("App");
foreach (XmlNode node in nodes)
{
if (node != null)
{
//save to DB
}
}
这就是我以前单独保存注释的内容:
This is what I used to save comments separately:
XDocument Doc=XDocument.Load("https://abc/file.xml");
foreach (var node in Doc.Descendants("App").Nodes())
{
if (node is XComment)
{
//save comments
}
推荐答案
我发现了如何做到这一点:
I found out how to do so:
XDocument Doc=XDocument.Load("https://abc/file.xml");
foreach (var node in Doc.Descendants("App").Nodes())
{
if (node is XComment)
{
//assign comment to variable
//save both to DB -- saving here because comments are after the data nodes.
}
else if(node is XComment==false)
{
//assign the needed id to variable
}
这篇关于Xml与节点一起注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!