I want to save certain classes and since xml-serialization won't do it in my case i'm saving the values manually into an xml-document. Works fine, but FxCop doesn't like it and since FxCop normally gives good advice and reasons why i shouldn't do things in a certain way i try to keep it happy.
This time, i dont understand how this is an improvement.
This is what i had:
public void Save()
{
XmlDocument doc = new XmlDocument();
XmlNode XmlNodeJob = doc.CreateElement("Job");
doc.AppendChild(XmlNodeJob);
OtherclassSave2(XmlNodeJob);//Node as Parameter
}
public void OtherclassSave2(XmlNode node)
{
}
And this is what FxCop complained: "Modify member 'OtherclassSave2(XmlNode)' so that it no longer exposes the concrete type 'XmlNode'. Use IXPathNavigable to represent XML data sources."
And now my awesome solution:
public void Save() { XmlDocument doc = new XmlDocument(); XmlNode XmlNodeJob = doc.CreateElement("Job"); doc.AppendChild(XmlNodeJob); OtherclassSave2(XmlNodeJob.CreateNavigator());//Interface from a node's navigator } public void OtherclassSave2(IXPathNavigable nav) { XmlNode node = (XmlNode)(nav.CreateNavigator().UnderlyingObject); }
This way i get my node in the other method and FxCop is happy, but i really don't see the improvement and i need a node to add things in it, not something to read.
I though about changing void SaveInThisNode(XmlNode) into a XmlNode GetMeTheNode() but to create nodes via CreateElements, i need the XmlDocument-object which i am not allowed to use as a parameter, but i could create new XmlDocuments in every step, fine.
My solution was simple and worked fine for everything i wanted it to do, but FxCop does not seem to allow solutions that are not obviously worse and more complicated.