We are parsing an xml and after serializing them ,it will be stored in database.
Our XML is look like below.
<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>
We need to store the value in SampleType_PK_ID in a string and then remove both
SampleTypes and SampleType_PK_ID node.
I am trying to delete it like below.
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.
}
But strSampleTypePKID is returning as empty string. What am I missing here. How to take the child to child node value and then delete it along with it’s immediate parent?
You’re looking for the inner child node “SampleType_PK_ID” at the wrong level.
If you put a breakpoint at the line
strSampleTypePKID = ChildList[j].InnerText;, you can see that it’s never been executed.Try the following instead: