LINQ to XML simple example

You can read the configuration file as a way to use


Sample file: https: //msdn.microsoft.com/zh-tw/library/bb669158.aspx
teaching Description: https: //msdn.microsoft.com/zh-tw/library/bb387041.aspx

static void ReadFromXML()
{
    try
    {
        XElement root = XElement.Load("../../App_Data/XML01.xml");
        XNamespace aw = "http://www.adventure-works.com";

        Console.WriteLine(root.Attribute(aw + "PurchaseOrderNumber").Value);
        Console.WriteLine(root.Attribute(aw + "OrderDate").Value);

        IEnumerable
  
  
   
    address =
            from el in root.Elements(aw + "Address")
            where (string)el.Attribute(aw + "Type") == "Billing"
            select el;

        foreach (XElement el in address)
            Console.WriteLine(el);

        foreach (XElement el in address)
            Console.WriteLine(el.Element(aw+"Zip").Value);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

  
  

Output:

99503
1999-10-20

  
  
  
   
   
    
    Tai Yee
   
   
  
   
   
    
    8 Oak Avenue
   
   
  
   
   
    
    Old Town
   
   
  
   
   
    
    PA
   
   
  
   
   
    
    95819
   
   
  
   
   
    
    USA
   
   

  
  
95819

Original: Big Box  LINQ to XML simple example


Guess you like

Origin www.cnblogs.com/chinatrump/p/11516354.html