P
Peted
Using Vs 2008 C#
Im using this code segment bellow, which works and does what im after,
but im wondering if it is actually good code, or just inefficient and
if there is a better way to achieve the same result.
Essentially
1. recieve and xml string
2. check multiple <PCSServiceCode> nodes to see if at least 1
<ServiceCode> node exists
3. if not then remove that whole <PCSServiceCode> from the xml
string/doc
4.pass the modified xml string to be deserialized.
If anyone has any suggests or improvements, comments are welcome
regards
Peted
public static PCSBillingEventDataType Deserialize(string xml)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
foreach (XmlNode pcsServiceCodeNode in
xmlDoc.SelectNodes("/PCSBillingEventData/ProblemPageList/PCSProblemPage/ServiceCodeList/PCSServiceCode"))
{
bool pcsNodeRemove = true;
foreach (XmlNode node in
pcsServiceCodeNode.ChildNodes)
{
if (node.Name.ToString() == "ServiceCode")
pcsNodeRemove = false;
}
if (pcsNodeRemove)
{
pcsServiceCodeNode.ParentNode.RemoveChild(pcsServiceCodeNode);
}
}
// assign new xml data for deserialization.
xml = xmlDoc.InnerXml;
/* deserialize xml */
XmlSerializer serializer = new
XmlSerializer(typeof(PCSBillingEventDataType));
using (StringReader sr = new StringReader(xml))
{
PCSBillingEventDataType data =
(PCSBillingEventDataType)serializer.Deserialize(sr);
return data;
}
}
catch (Exception ex) { throw; }
}
}
Im using this code segment bellow, which works and does what im after,
but im wondering if it is actually good code, or just inefficient and
if there is a better way to achieve the same result.
Essentially
1. recieve and xml string
2. check multiple <PCSServiceCode> nodes to see if at least 1
<ServiceCode> node exists
3. if not then remove that whole <PCSServiceCode> from the xml
string/doc
4.pass the modified xml string to be deserialized.
If anyone has any suggests or improvements, comments are welcome
regards
Peted
public static PCSBillingEventDataType Deserialize(string xml)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
foreach (XmlNode pcsServiceCodeNode in
xmlDoc.SelectNodes("/PCSBillingEventData/ProblemPageList/PCSProblemPage/ServiceCodeList/PCSServiceCode"))
{
bool pcsNodeRemove = true;
foreach (XmlNode node in
pcsServiceCodeNode.ChildNodes)
{
if (node.Name.ToString() == "ServiceCode")
pcsNodeRemove = false;
}
if (pcsNodeRemove)
{
pcsServiceCodeNode.ParentNode.RemoveChild(pcsServiceCodeNode);
}
}
// assign new xml data for deserialization.
xml = xmlDoc.InnerXml;
/* deserialize xml */
XmlSerializer serializer = new
XmlSerializer(typeof(PCSBillingEventDataType));
using (StringReader sr = new StringReader(xml))
{
PCSBillingEventDataType data =
(PCSBillingEventDataType)serializer.Deserialize(sr);
return data;
}
}
catch (Exception ex) { throw; }
}
}