Csharp/C Sharp/XML LINQ/Attribute

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Accessing All of an Element"s Attributes with the Attributes Method

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       foreach (XAttribute attr in firstParticipant.Attributes()) {
           Console.WriteLine(attr);
       }
   }

}

</source>


Accessing an Attribute with the Attribute Method

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(firstParticipant.Attribute("type").Value);
   }

}

</source>


Accessing an Element"s Attributes Using the Attributes Method

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
        new XDeclaration("1.0", "UTF-8", "yes"),
        new XDocumentType("Books", null, "Books.dtd", null),
        new XProcessingInstruction("Book", "out-of-print"),
        new XElement("Books", firstParticipant =
          new XElement("Book",
          new XComment("This is a new author."),
          new XProcessingInstruction("AuthorHandler", "new"),
        new XAttribute("type", "Author"),
        new XElement("FirstName", "J"),
        new XElement("LastName", "R")),
      new XElement("Book",
        new XAttribute("type", "Author"),
        new XElement("FirstName", "E"),
        new XElement("LastName", "B"))));
       foreach (XAttribute attr in firstParticipant.Attributes()) {
           Console.WriteLine(attr);
       }
   }

}

</source>


Accessing an Element"s First Attribute with the FirstAttribute Property

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XAttribute("language", "English"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(firstParticipant.FirstAttribute);
   }

}

</source>


Accessing the Last Attribute with the LastAttribute Property

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XAttribute("language", "English"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(firstParticipant.LastAttribute);
   }

}

</source>


Accessing the Next Attribute with the NextAttribute Property

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XAttribute("language", "English"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(firstParticipant.FirstAttribute.NextAttribute);
   }

}

</source>


Accessing the Previous Attribute with the PreviousAttribute Property

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XAttribute("language", "English"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(firstParticipant.FirstAttribute.NextAttribute.PreviousAttribute);
   }

}

</source>


Calling the First Attributes Prototype

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = new XDocument(
        new XElement("Books",
          new XElement("Book",
           new XAttribute("type", "Author"),
           new XElement("FirstName", "A"),
          new XElement("LastName", "B")),
          new XElement("Book",
           new XAttribute("type", "Author"),
          new XElement("FirstName", "C"),
          new XElement("LastName", "D"))));
       IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book");
       foreach (XElement element in elements) {
           Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value);
       }
       foreach (XAttribute attribute in elements.Attributes()) {
           Console.WriteLine("Attribute: {0} : value = {1}",
            attribute.Name, attribute.Value);
       }
   }

}

</source>


Calling the Remove

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = new XDocument(
        new XElement("Books",
          new XElement("Book",
           new XAttribute("type", "Author"),
          new XComment("This is a new author."),
           new XElement("FirstName", "A"),
          new XElement("LastName", "B")),
          new XElement("Book",
           new XAttribute("type", "Author"),
          new XElement("FirstName", "C"),
          new XElement("LastName", "D"))));
       IEnumerable<XAttribute> attributes = xDocument.Element("Books").Elements("Book").Attributes();
       foreach (XAttribute attribute in attributes) {
           Console.WriteLine("Source attribute: {0} : value = {1}", attribute.Name, attribute.Value);
       }
       attributes.Remove();
       Console.WriteLine(xDocument);
   }

}

</source>


Calling the Second Attributes Prototype

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XDocument xDocument = new XDocument(
        new XElement("Books",
          new XElement("Book",
           new XAttribute("type", "Author"),
           new XElement("FirstName", "A"),
          new XElement("LastName", "B")),
          new XElement("Book",
           new XAttribute("type", "Author"),
          new XElement("FirstName", "C"),
          new XElement("LastName", "D"))));
       IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book");
       foreach (XElement element in elements) {
           Console.WriteLine("Source element: {0} : value = {1}", element.Name, element.Value);
       }
       foreach (XAttribute attribute in elements.Attributes("type")) {
           Console.WriteLine("Attribute: {0} : value = {1}",
            attribute.Name, attribute.Value);
       }
   }

}

</source>


Changing an Attribute"s Value

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument  = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(xDocument);
       firstParticipant.Attribute("experience").Value = "beginner";
       Console.WriteLine(xDocument);
   }

}

</source>


Get Attribute with namespace

<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml.Linq; class Program {

   static void Main(string[] args) {
       XElement xml = XElement.Load("Hello_XLINQ.xml");
       XNamespace w = "http://schemas.microsoft.ru/office/word/2003/wordml";
       XElement defaultFonts = xml.Descendants(w + "defaultFonts").First();
       Console.WriteLine("Default Fonts: {0}", (string)defaultFonts.Attribute(w + "ascii"));
   }

}

</source>


Removing All of an Element"s Attributes

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XElement("FirstName", "A"),
             new XElement("LastName", "B"))));
       Console.WriteLine(xDocument);
       firstParticipant.Attributes().Remove();
       Console.WriteLine(xDocument);
   }

}

</source>


Removing an Attribute

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(xDocument);
       firstParticipant.Attribute("type").Remove();
       Console.WriteLine(xDocument);
   }

}

</source>


Using SetAttributeValue to Add, Delete, and Update Attributes

<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {

   public static void Main() {
       XElement firstParticipant;
       XDocument xDocument = new XDocument(
         new XElement("Books", firstParticipant =
           new XElement("Book",
             new XAttribute("type", "Author"),
             new XAttribute("experience", "first-time"),
             new XElement("FirstName", "J"),
             new XElement("LastName", "R"))));
       Console.WriteLine(xDocument);
       firstParticipant.SetAttributeValue("type", "beginner");
       firstParticipant.SetAttributeValue("language", "English");
       firstParticipant.SetAttributeValue("experience", null);
       Console.WriteLine(xDocument);
   }

}

</source>