Converting between XDocument and XmlDocument, XElement and XmlElement

http://brianary.blogspot.com/2010/02/converting-between-xdocument-and.html

 public static XmlDocument ToXmlDocument(this XDocument xdoc)
  {
    var xmldoc = new XmlDocument();
    xmldoc.Load(xdoc.CreateReader());
    return xmldoc;
  }
  
  ///
  /// Converts an XmlDocument to an XDocument.
  ///

  /// The XmlDocument to convert.
  /// The equivalent XDocument.
  public static XDocument ToXDocument(this XmlDocument xmldoc)
  {
    return XDocument.Load(xmldoc.CreateNavigator().ReadSubtree());
  }
  
  ///
  /// Converts an XElement to an XmlElement.
  ///

  /// The XElement to convert.
  /// The equivalent XmlElement.
  public static XmlElement ToXmlElement(this XElement xelement)
  {
    return new XmlDocument().ReadNode(xelement.CreateReader()) as XmlElement;
  }
  
  ///
  /// Converts an XmlElement to an XElement.
  ///

  /// The XmlElement to convert.
  /// The equivalent XElement.
  public static XElement ToXElement(this XmlElement xmlelement)
  {
    return XElement.Load(xmlelement.CreateNavigator().ReadSubtree());
  }



public XmlElement doAvail(XmlDocument xmlRequest, XmlDocument xmlFilter)
{

XmlElement xmlResponse = null;
try
{

XmlSelect xws = new XmlSelect();
NetworkCredential credentials = new NetworkCredential("PCC69TX", "Badur69tx");
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(xws.Url), "Basic", credentials);
xws.Credentials = cc;

xws.PreAuthenticate = true;
xmlResponse = xws.SubmitXml("DynGalileoCopy_69TX", xmlRequest.DocumentElement, xmlFilter.DocumentElement);


XElement x = XmlLinqConversionExtensions.ToXElement(xmlResponse);

System.Xml.Linq.XDocument xmlDoc = new System.Xml.Linq.XDocument( x);

var persons = from person in xmlDoc.Descendants("AvailFlt")
select new
{
AirV = person.Element("AirV").Value,
FltNum = person.Element("FltNum").Value,
StartDt = person.Element("StartDt").Value,
StartAirp = person.Element("StartAirp").Value,
EndAirp = person.Element("EndAirp").Value,
NumStops = person.Element("NumStops").Value,
StartTm = person.Element("StartTm").Value,
EndTm = person.Element("EndTm").Value,
JrnyTm = person.Element("JrnyTm").Value,
EndDt = person.Element("EndDt").Value,

};
rptUserList.DataSource = persons;
rptUserList.DataBind();

}
catch (Exception ex)
{
// TextBox2.Text = "Error Message : \n " + ex.Message + "\n\n\n Stack Trace : \n" + ex.StackTrace;
}
return xmlResponse;
}

public XmlDocument BuildRequest()
{

XmlDocument doc = new XmlDocument();
DateTime tomorrow = DateTime.Today.AddDays(90.0);
string req = "\r\n";
req += " \r\n";
req += " \r\n";
req += " 1\r\n";
req += " \r\n";
req += " " + tomorrow.ToString("yyyyMMdd") + "\r\n";
req += " DEN\r\n";
req += " ORD\r\n";
req += " 1200\r\n";
req += " D\r\n";
req += " 0800\r\n";
req += " 1400\r\n";
req += " \r\n";
req += " B\r\n";
req += " B\r\n";
req += " \r\n";
req += "
\r\n";
req += "
\r\n";
req += "
\r\n";
doc.LoadXml(req);
return doc;

}

public XmlDocument BuildFilter()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<_/>");
return doc;
}

No comments:

Post a Comment