PivotalTrackerAPI/Domain/Services/PivotalService.cs
/// <summary>
/// Strips out content in a model instance that should not be submitted to Pivotal or that is blank (optional)
/// </summary>
/// <param name="xml">The xml document to clean up</param>
/// <param name="rootXpath">The root node to begin searching for elements</param>
/// <param name="ignoredNodes">A list of nodes that should be removed</param>
/// <param name="removeIfHasAttributes">Controls whether the cleanup should remove any attributes and remove any elements that have attributes (useful when the only reason for an attribute is the deserialization putting a nil attribute in the emitted xml)</param>
/// <returns>The cleaned xml as a string, ready for submission</returns>
Strips out content in a model instance that should not be submitted to Pivotal or that is blank (optional)
Strips out content in a model instance that should not be submitted to Pivotal or that is blank ( optional )
public static string CleanXmlForSubmission(XmlDocument xml, string rootXpath, string[] ignoredNodes, bool removeIfHasAttributes)
    {
      XmlNode toRemove = null;
      XmlNode rootNode = xml.SelectSingleNode(rootXpath);
      if (rootNode != null)
      {
        foreach (string s in ignoredNodes)
        {
          toRemove = xml.SelectSingleNode(rootXpath + s);
          if (toRemove != null)
            rootNode.RemoveChild(toRemove);
        }
        if (removeIfHasAttributes)
        {
          XmlNodeList emptyNodes = rootNode.ChildNodes;
          int nodeCount = emptyNodes.Count;
          for (int i = 0; i < nodeCount; i++)
          {
            if (emptyNodes[i] != null && emptyNodes[i].Attributes.Count > 0)
            {
              rootNode.RemoveChild(emptyNodes[i]);
              i--;
            }
            if (i >= emptyNodes.Count)
              break;
          }
          rootNode.Attributes.RemoveAll();
        }
      }
      return xml.DocumentElement.OuterXml;
    }
public static string CleanXmlForSubmission ( XmlDocument xml , string rootXpath , string [ ] ignoredNodes , bool removeIfHasAttributes ) { XmlNode toRemove = null ; XmlNode rootNode = xml . SelectSingleNode ( rootXpath ) ; if ( rootNode != null ) { foreach ( string s in ignoredNodes ) { toRemove = xml . SelectSingleNode ( rootXpath + s ) ; if ( toRemove != null ) rootNode . RemoveChild ( toRemove ) ; } if ( removeIfHasAttributes ) { XmlNodeList emptyNodes = rootNode . ChildNodes ; int nodeCount = emptyNodes . Count ; for ( int i = 0 ; i < nodeCount ; i ++ ) { if ( emptyNodes [ i ] != null && emptyNodes [ i ] . Attributes . Count > 0 ) { rootNode . RemoveChild ( emptyNodes [ i ] ) ; i -- ; } if ( i >= emptyNodes . Count ) break ; } rootNode . Attributes . RemoveAll ( ) ; } } return xml . DocumentElement . OuterXml ; }
Strips out content in a model instance that should not be submitted to Pivotal or that is blank (optional)
Strips out content in a model instance that should not be submitted to Pivotal or that is blank ( optional )
//xml.RemoveChild(toRemove); // The only way a node in the story would have an attribute is if it is null
The cleaned xml as a string, ready for submission
The cleaned xml as a string ready for submission
xml
XmlDocument
The xml document to clean up
The xml document to clean up
rootXpath
string
The root node to begin searching for elements
The root node to begin searching for elements
ignoredNodes
string[]
A list of nodes that should be removed
A list of nodes that should be removed
removeIfHasAttributes
bool
Controls whether the cleanup should remove any attributes and remove any elements that have attributes (useful when the only reason for an attribute is the deserialization putting a nil attribute in the emitted xml)
Controls whether the cleanup should remove any attributes and remove any elements that have attributes ( useful when the only reason for an attribute is the deserialization putting a nil attribute in the emitted xml )