0
我们解析一个xml,并在序列化它们之后,它将被存储在数据库中。如何将子节点存储到子节点并删除直接父节点?
我们的XML如下所示。
<SampleTypeService>
<Name>sample1</Name>
<URL>sample1</URL>
<SampleTypeService_PK_ID>225d0266-e83a-44b8-88fc-700f6570d530</SampleTypeService_PK_ID>
<SampleTypes>
<SampleType_PK_ID>ef1d8c40-72ce-48d8-b252-9b521e96fa74</SampleType_PK_ID>
</SampleTypes>
</SampleTypeService>
<SampleTypeService>
<Name>sample2</Name>
<URL>sample2</URL>
<SampleTypeService_PK_ID>225reg66-e83a-44b8-88fc-700f6570d530</SampleTypeService_PK_ID>
<SampleTypes>
<SampleType_PK_ID>gh4d8c40-72ce-48d8-b252-9b521e96fa74</SampleType_PK_ID>
</SampleTypes>
</SampleTypeService>
我们需要的价值SampleType_PK_ID
存储在一个字符串,然后删除这两个
SampleTypes
和SampleType_PK_ID
节点。
我想删除它,如下所示。
foreach (XmlNode SampleNode in SampleList)
{
XmlNodeList ChildList = SampleNode.ChildNodes;
for (int j = 0; j < ChildList.Count; j++)
{
if (ChildList[j].LocalName == "SampleType_PK_ID>")
{
strSampleTypePKID = ChildList[j].InnerText;
if (strSampleTypePKID != null)
{
SampleNode.ParentNode.RemoveChild(ChildList[j]);
j--;
}
}
}
testString = SampleNode.OuterXml;
Console.WriteLine("1):" + strSampleTypePKID);
//Code to serialize and store in database is here.
}
但strSampleTypePKID
返回为空字符串。我在这里错过了什么。如何将孩子带到子节点值,然后将其与其直接父代一起删除?
谢谢回答的清洁版本。看起来不错,比拥有多个循环。 – Samselvaprabu