Use Recursion to Clear out Textboxes : Listing 2

C# does not include the extra operators of the VB literal XML syntax. Converting between C# and Visual Basic requires converting between the axis shortcuts .<>, …<>, @ and the Elements, Descendants, and Attributes axis methods. You’ll also need to convert between XML literals and the corresponding XDocument, XElement and XAttribute constructors and directly manage namespaces.

public class ConvertXML
{
   private XNamespace co = "http:CustomersAndOrder";
   private XName fullAddressName;
   private XName customerName;
   private XName orderName;
   private XName shipRegionName;

   public ConvertXML()
   {
      fullAddressName = co + "FullAddress";
      customerName = co + "Customer";
      orderName = co + "Order";
      shipRegionName = co + "ShipRegion";
   }
   public void Test()
   {
      var xDoc = XDocument.Load("InputFileName..xml");
      var out3 = new XDocument(BuildChild3(xDoc.Root));
      var writer = XmlWriter.Create("OutputFIleName.xml");
      out3.WriteTo(writer);
      writer.Flush();
      writer.Close();
   }
   private XElement BuildChild3(XElement element)
   {
      if (element.Name == customerName)
      {
         var q = (from child in element.Elements()
            where child.Name != fullAddressName
            select child).Union(
               from grandchild in 
               element.Elements(fullAddressName).Elements()
            select grandchild);
         return new XElement(element.Name, element.Attributes(),
            from child in q select BuildChild3(child),
            from node in element.Nodes() where !(node is XElement) 
            select node);
      }
      else if (element.Name == orderName)
      {
         if (element.Descendants(shipRegionName).First().Value == "OR")
         { return null; }
         else
         { return element; }
      }
      else
      {
         return new XElement(element.Name, element.Attributes(),
             from node in element.Nodes() where !(node is XElement) 
                select node,
             from child in element.Elements() select BuildChild3(child));
      }
   }
}
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.