Condividi tramite


Come trasformare la forma di un albero XML (LINQ to XML)

Per forma di un documento XML si intendono i relativi nomi di elemento, nomi di attributi, nonché le caratteristiche della relativa gerarchia.

In alcuni casi è necessario modificare la forma di un documento XML. Ad esempio, può essere necessario inviare un documento XML esistente a un altro sistema che richiede nomi di elemento e di attributo diversi. In tal caso, è possibile scorrere il documento, eliminando e rinominando gli elementi a seconda dei casi, tuttavia l'uso della costruzione funzionale consente di ottenere codice più leggibile e conservabile. Per altre informazioni sulla costruzione funzionale, vedere Costruzione funzionale.

Il primo esempio di questo articolo cambia l'organizzazione di un documento XML. Gli elementi complessi vengono spostati da un punto all'altro dell'albero.

Il secondo esempio crea un documento XML la cui forma è diversa da quella del documento di origine. Omette alcuni elementi, ne rinomina altri e cambia la combinazione di maiuscole e minuscole dei nomi degli elementi.

Esempio: Usare espressioni di query incorporate per cambiare la forma di un albero XML

L'esempio seguente cambia la forma di un file XML usando espressioni di query incorporate.

Il documento XML di origine di questo esempio, File XML di esempio: Clienti e ordini, include un elemento Customers sotto l'elemento Root che contiene tutti i clienti. Include inoltre un elemento Orders sotto l'elemento Root che contiene tutti gli ordini. L'esempio crea un nuovo albero XML in cui gli ordini per ogni clienti sono contenuti in un elemento Orders all'interno dell'elemento Customer. Il documento originale include inoltre l'elemento CustomerID nell'elemento Order, che verrà omesso dal nuovo albero.

XElement co = XElement.Load("CustomersOrders.xml");
XElement newCustOrd =
    new XElement("Root",
        from cust in co.Element("Customers").Elements("Customer")
        select new XElement("Customer",
            cust.Attributes(),
            cust.Elements(),
            new XElement("Orders",
                from ord in co.Element("Orders").Elements("Order")
                where (string)ord.Element("CustomerID") == (string)cust.Attribute("CustomerID")
                select new XElement("Order",
                    ord.Attributes(),
                    ord.Element("EmployeeID"),
                    ord.Element("OrderDate"),
                    ord.Element("RequiredDate"),
                    ord.Element("ShipInfo")
                )
            )
        )
    );
Console.WriteLine(newCustOrd);
Dim co As XElement = XElement.Load("CustomersOrders.xml")
Dim newCustOrd = _
   <Root>
       <%= From cust In co.<Customers>.<Customer> _
           Select _
           <Customer>
               <%= cust.Attributes() %>
               <%= cust.Elements() %>
               <Orders>
                   <%= From ord In co.<Orders>.<Order> _
                       Where ord.<CustomerID>.Value = cust.@CustomerID _
                       Select _
                       <Order>
                           <%= ord.Attributes() %>
                           <%= ord.<EmployeeID> %>
                           <%= ord.<OrderDate> %>
                           <%= ord.<RequiredDate> %>
                           <%= ord.<ShipInfo> %>
                       </Order> _
                   %>
               </Orders>
           </Customer> _
       %>
   </Root>
Console.WriteLine(newCustOrd)

Nell'esempio viene prodotto l'output seguente:

<Root>
  <Customer CustomerID="GREAL">
    <CompanyName>Great Lakes Food Market</CompanyName>
    <ContactName>Howard Snyder</ContactName>
    <ContactTitle>Marketing Manager</ContactTitle>
    <Phone>(503) 555-7555</Phone>
    <FullAddress>
      <Address>2732 Baker Blvd.</Address>
      <City>Eugene</City>
      <Region>OR</Region>
      <PostalCode>97403</PostalCode>
      <Country>USA</Country>
    </FullAddress>
    <Orders />
  </Customer>
  <Customer CustomerID="HUNGC">
    <CompanyName>Hungry Coyote Import Store</CompanyName>
    <ContactName>Yoshi Latimer</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Phone>(503) 555-6874</Phone>
    <Fax>(503) 555-2376</Fax>
    <FullAddress>
      <Address>City Center Plaza 516 Main St.</Address>
      <City>Elgin</City>
      <Region>OR</Region>
      <PostalCode>97827</PostalCode>
      <Country>USA</Country>
    </FullAddress>
    <Orders />
  </Customer>
  ...
</Root>

Esempio: Creare un documento la cui forma è diversa da quella del documento di origine

In questo esempio vengono rinominati alcuni elementi e convertiti alcuni attributi in elementi. Chiama ConvertAddress che restituisce un elenco di oggetti XElement. L'argomento del metodo è una query che determina l'elemento complesso Address in cui il valore dell'attributo Type è "Shipping". L'esempio usa il documento XML seguente: File XML di esempio: Ordine di acquisto tipico.

static IEnumerable<XElement> ConvertAddress(XElement add)
{
    List<XElement> fragment = new List<XElement>() {
        new XElement("NAME", (string)add.Element("Name")),
        new XElement("STREET", (string)add.Element("Street")),
        new XElement("CITY", (string)add.Element("City")),
        new XElement("ST", (string)add.Element("State")),
        new XElement("POSTALCODE", (string)add.Element("Zip")),
        new XElement("COUNTRY", (string)add.Element("Country"))
    };
    return fragment;
}

static void Main(string[] args)
{
    XElement po = XElement.Load("PurchaseOrder.xml");
    XElement newPo = new XElement("PO",
        new XElement("ID", (string)po.Attribute("PurchaseOrderNumber")),
        new XElement("DATE", (DateTime)po.Attribute("OrderDate")),
        ConvertAddress(
            (from el in po.Elements("Address")
            where (string)el.Attribute("Type") == "Shipping"
            select el)
            .First()
        )
    );
    Console.WriteLine(newPo);
}
Function ConvertAddress(ByVal add As XElement) As IEnumerable(Of XElement)
    Dim fragment = New List(Of XElement)
    fragment.Add(<NAME><%= add.<Name>.Value %></NAME>)
    fragment.Add(<STREET><%= add.<Street>.Value %></STREET>)
    fragment.Add(<CITY><%= add.<City>.Value %></CITY>)
    fragment.Add(<ST><%= add.<State>.Value %></ST>)
    fragment.Add(<POSTALCODE><%= add.<Zip>.Value %></POSTALCODE>)
    fragment.Add(<COUNTRY><%= add.<Country>.Value %></COUNTRY>)
    Return fragment
End Function

Sub Main()
    Dim po As XElement = XElement.Load("PurchaseOrder.xml")
    Dim newPo As XElement = _
        <PO>
            <ID><%= po.@PurchaseOrderNumber %></ID>
            <DATE><%= CDate(po.@OrderDate) %></DATE>
            <%= _
                ConvertAddress( _
                (From el In po.<Address> _
                Where el.@Type = "Shipping" _
                Select el) _
                .First() _
                ) _
            %>
        </PO>
    Console.WriteLine(newPo)
End Sub

Nell'esempio viene prodotto l'output seguente:

<PO>
  <ID>99503</ID>
  <DATE>1999-10-20T00:00:00</DATE>
  <NAME>Ellen Adams</NAME>
  <STREET>123 Maple Street</STREET>
  <CITY>Mill Valley</CITY>
  <ST>CA</ST>
  <POSTALCODE>10999</POSTALCODE>
  <COUNTRY>USA</COUNTRY>
</PO>

Vedi anche