From 767f684cd4ee5fd11d678cee891f1670883c94d7 Mon Sep 17 00:00:00 2001 From: jameshkramer Date: Tue, 28 Jan 2020 15:27:57 -0800 Subject: [PATCH 1/2] 6a). Add topic files 7 to 8 --- .openpublishing.redirection.json | 16 ++ .../linq/xattribute-class-overview.md | 78 ---------- .../concepts/linq/xelement-class-overview.md | 94 ------------ docs/standard/xattribute-class-overview.md | 105 ++++++++++++++ docs/standard/xelement-class-overview.md | 137 ++++++++++++++++++ .../linq/xattribute-class-overview.md | 73 ---------- .../concepts/linq/xelement-class-overview.md | 97 ------------- 7 files changed, 258 insertions(+), 342 deletions(-) delete mode 100644 docs/csharp/programming-guide/concepts/linq/xattribute-class-overview.md delete mode 100644 docs/csharp/programming-guide/concepts/linq/xelement-class-overview.md create mode 100644 docs/standard/xattribute-class-overview.md create mode 100644 docs/standard/xelement-class-overview.md delete mode 100644 docs/visual-basic/programming-guide/concepts/linq/xattribute-class-overview.md delete mode 100644 docs/visual-basic/programming-guide/concepts/linq/xelement-class-overview.md diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index ef5886024bc82..8471ce6d690b5 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -1229,6 +1229,14 @@ "redirect_url": "/dotnet/standard/linq/linq-xml-classes-overview", "redirect_document_id": true }, + { + "source_path": "docs/csharp/programming-guide/concepts/linq/xattribute-class-overview.md", + "redirect_url": "/dotnet/standard/linq/xattribute-class-overview" + }, + { + "source_path": "docs/csharp/programming-guide/concepts/linq/xelement-class-overview.md", + "redirect_url": "/dotnet/standard/linq/xelement-class-overview" + }, { "source_path": "docs/csharp/programming-guide/concepts/threading/how-to-use-a-thread-pool.md", "redirect_url": "/dotnet/api/system.threading.threadpool.queueuserworkitem" @@ -4208,6 +4216,14 @@ "source_path": "docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-classes-overview.md", "redirect_url": "/dotnet/standard/linq/linq-xml-classes-overview" }, + { + "source_path": "docs/visual-basic/programming-guide/concepts/linq/xattribute-class-overview.md", + "redirect_url": "/dotnet/standard/linq/xattribute-class-overview" + }, + { + "source_path": "docs/visual-basic/programming-guide/concepts/linq/xelement-class-overview.md", + "redirect_url": "/dotnet/standard/linq/xelement-class-overview" + }, { "source_path": "docs/visual-basic/programming-guide/concepts/threading/how-to-use-a-thread-pool.md", "redirect_url": "/dotnet/api/system.threading.threadpool.queueuserworkitem" diff --git a/docs/csharp/programming-guide/concepts/linq/xattribute-class-overview.md b/docs/csharp/programming-guide/concepts/linq/xattribute-class-overview.md deleted file mode 100644 index e54e860e58c2a..0000000000000 --- a/docs/csharp/programming-guide/concepts/linq/xattribute-class-overview.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: "XAttribute Class Overview (C#)" -ms.date: 07/20/2015 -ms.assetid: 5a630f24-f9ad-400e-831e-c14ebfc9e142 ---- -# XAttribute Class Overview (C#) -Attributes are name/value pairs that are associated with an element. The class represents XML attributes. - -## Overview - Working with attributes in [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is similar to working with elements. Their constructors are similar. The methods that you use to retrieve collections of them are similar. A LINQ query expression for a collection of attributes looks very similar to a LINQ query expression for a collection of elements. - - The order in which attributes were added to an element is preserved. That is, when you iterate through the attributes, you see them in the same order that they were added. - -## The XAttribute Constructor - The following constructor of the class is the one that you will most commonly use: - -|Constructor|Description| -|-----------------|-----------------| -|`XAttribute(XName name, object content)`|Creates an object. The `name` argument specifies the name of the attribute; `content` specifies the content of the attribute.| - -### Creating an Element with an Attribute - The following code shows the common task of creating an element that contains an attribute: - -```csharp -XElement phone = new XElement("Phone", - new XAttribute("Type", "Home"), - "555-555-5555"); -Console.WriteLine(phone); -``` - - This example produces the following output: - -```xml -555-555-5555 -``` - -### Functional Construction of Attributes - You can construct objects in-line with the construction of objects, as follows: - -```csharp -XElement c = new XElement("Customers", - new XElement("Customer", - new XElement("Name", "John Doe"), - new XElement("PhoneNumbers", - new XElement("Phone", - new XAttribute("type", "home"), - "555-555-5555"), - new XElement("Phone", - new XAttribute("type", "work"), - "666-666-6666") - ) - ) -); -Console.WriteLine(c); -``` - - This example produces the following output: - -```xml - - - John Doe - - 555-555-5555 - 666-666-6666 - - - -``` - -### Attributes Are Not Nodes - There are some differences between attributes and elements. objects are not nodes in the XML tree. They are name/value pairs associated with an XML element. In contrast to the Document Object Model (DOM), this more closely reflects the structure of XML. Although objects are not actually nodes in the XML tree, working with objects is very similar to working with objects. - - This distinction is primarily important only to developers who are writing code that works with XML trees at the node level. Many developers will not be concerned with this distinction. - -## See also - -- [LINQ to XML Programming Overview (C#)](./linq-to-xml-overview.md) diff --git a/docs/csharp/programming-guide/concepts/linq/xelement-class-overview.md b/docs/csharp/programming-guide/concepts/linq/xelement-class-overview.md deleted file mode 100644 index e90d2dcbe125e..0000000000000 --- a/docs/csharp/programming-guide/concepts/linq/xelement-class-overview.md +++ /dev/null @@ -1,94 +0,0 @@ ---- -title: "XElement Class Overview (C#)" -ms.date: 07/20/2015 -ms.assetid: 2b9f0cd8-a1d1-4037-accf-0f38a410fa11 ---- -# XElement Class Overview (C#) -The class is one of the fundamental classes in [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)]. It represents an XML element. You can use this class to create elements; change the content of the element; add, change, or delete child elements; add attributes to an element; or serialize the contents of an element in text form. You can also interoperate with other classes in , such as , , and . - -This topic describes the functionality provided by the class. - -## Constructing XML Trees - You can construct XML trees in a variety of ways, including the following: - -- You can construct an XML tree in code. For more information, see [Creating XML Trees (C#)](./linq-to-xml-overview.md). - -- You can parse XML from various sources, including a , text files, or a Web address (URL). For more information, see [Parsing XML (C#)](./how-to-parse-a-string.md). - -- You can use an to populate the tree. For more information, see . - -- If you have a module that can write content to an , you can use the method to create a writer, pass the writer to the module, and then use the content that is written to the to populate the XML tree. - - However, the most common way to create an XML tree is as follows: - -```csharp -XElement contacts = - new XElement("Contacts", - new XElement("Contact", - new XElement("Name", "Patrick Hines"), - new XElement("Phone", "206-555-0144"), - new XElement("Address", - new XElement("Street1", "123 Main St"), - new XElement("City", "Mercer Island"), - new XElement("State", "WA"), - new XElement("Postal", "68042") - ) - ) - ); -``` - - Another very common technique for creating an XML tree involves using the results of a LINQ query to populate an XML tree, as shown in the following example: - -```csharp -XElement srcTree = new XElement("Root", - new XElement("Element", 1), - new XElement("Element", 2), - new XElement("Element", 3), - new XElement("Element", 4), - new XElement("Element", 5) -); -XElement xmlTree = new XElement("Root", - new XElement("Child", 1), - new XElement("Child", 2), - from el in srcTree.Elements() - where (int)el > 2 - select el -); -Console.WriteLine(xmlTree); -``` - - This example produces the following output: - -```xml - - 1 - 2 - 3 - 4 - 5 - -``` - -## Serializing XML Trees - You can serialize the XML tree to a , a , or an . - - For more information, see [Serializing XML Trees (C#)](./preserving-white-space-while-serializing.md). - -## Retrieving XML Data via Axis Methods - You can use axis methods to retrieve attributes, child elements, descendant elements, and ancestor elements. LINQ queries operate on axis methods, and provide several flexible and powerful ways to navigate through and process an XML tree. - - For more information, see [LINQ to XML Axes (C#)](./linq-to-xml-axes-overview.md). - -## Querying XML Trees - You can write LINQ queries that extract data from an XML tree. - - For more information, see [Querying XML Trees (C#)](./how-to-find-an-element-with-a-specific-attribute.md). - -## Modifying XML Trees - You can modify an element in a variety of ways, including changing its content or attributes. You can also remove an element from its parent. - - For more information, see [Modifying XML Trees (LINQ to XML) (C#)](./in-memory-xml-tree-modification-vs-functional-construction-linq-to-xml.md). - -## See also - -- [LINQ to XML Programming Overview (C#)](serializing-to-files-textwriters-and-xmlwriters.md) diff --git a/docs/standard/xattribute-class-overview.md b/docs/standard/xattribute-class-overview.md new file mode 100644 index 0000000000000..f3642715731b5 --- /dev/null +++ b/docs/standard/xattribute-class-overview.md @@ -0,0 +1,105 @@ +--- +title: "XAttribute class overview" +ms.date: 07/20/2015 +dev_langs: + - "csharp" + - "vb" +ms.assetid: 5a630f24-f9ad-400e-831e-c14ebfc9e142 +--- + +# XAttribute class overview (C# and Visual Basic) + +Attributes are name/value pairs that are associated with an element. The class represents XML attributes. + +Working with attributes in LINQ to XML is similar to working with elements. Their constructors are similar. The methods that you use to retrieve collections of them are similar. A LINQ query expression for a collection of attributes looks very similar to a LINQ query expression for a collection of elements. + +The order in which attributes were added to an element is preserved. That is, when you iterate through the attributes, you see them in the same order that they were added. + +## The XAttribute constructor + +The following constructor of the class is the one that you will most commonly use: + +|Constructor|Description| +|-----------------|-----------------| +|`XAttribute(XName name, object content)`|Creates an object. The `name` argument specifies the name of the attribute; `content` specifies the content of the attribute.| + +## Example: Create an element with an attribute + +The following example shows the common task of creating an element that contains an attribute. + +```csharp +XElement phone = new XElement("Phone", + new XAttribute("Type", "Home"), + "555-555-5555"); +Console.WriteLine(phone); +``` + +```vb +Dim phone As XElement = 555-555-5555 +Console.WriteLine(phone) +``` + +The example produces this output: + +```xml +555-555-5555 +``` + +## Example: Functional construction of attributes + +You can construct objects in-line with the construction of objects. An example of code that does this is: + +```csharp +XElement c = new XElement("Customers", + new XElement("Customer", + new XElement("Name", "John Doe"), + new XElement("PhoneNumbers", + new XElement("Phone", + new XAttribute("type", "home"), + "555-555-5555"), + new XElement("Phone", + new XAttribute("type", "work"), + "666-666-6666") + ) + ) +); +Console.WriteLine(c); +``` + +```vb +Dim c As XElement = _ + + + John Doe + + 555-555-5555 + 666-666-6666 + + + +Console.WriteLine(c) +``` + +The example produces this output: + +```xml + + + John Doe + + 555-555-5555 + 666-666-6666 + + + +``` + +## Attributes are not nodes + +There are some differences between attributes and elements. objects are not nodes in the XML tree. They are name/value pairs associated with an XML element. In contrast to the Document Object Model (DOM), this more closely reflects the structure of XML. Although objects are not actually nodes in the XML tree, working with objects is very similar to working with objects. + +This distinction is primarily important only to developers who are writing code that works with XML trees at the node level. Many developers will not be concerned with this distinction. + +## See also + +- [LINQ to XML overview](./linq-xml-overview.md) diff --git a/docs/standard/xelement-class-overview.md b/docs/standard/xelement-class-overview.md new file mode 100644 index 0000000000000..4e2b99168d10b --- /dev/null +++ b/docs/standard/xelement-class-overview.md @@ -0,0 +1,137 @@ +--- +title: "XElement class overview" +ms.date: 07/20/2015 +dev_langs: + - "csharp" + - "vb" +ms.assetid: 2b9f0cd8-a1d1-4037-accf-0f38a410fa11 +--- + +# XElement class overview (C# and Visual Basic) + +The class is one of the fundamental classes in LINQ to XML. It represents an XML element. You can use this class to create elements; change the content of the element; add, change, or delete child elements; add attributes to an element; or serialize the contents of an element in text form. You can also interoperate with other classes in , such as , , and . + +This article describes the functionality provided by the class. + +## Construct XML trees + +You can construct XML trees in a variety of ways, including the following: + +- You can construct an XML tree in code. For more information, see [Create XML trees in C#](./create-xml-trees-csharp.md) and [Create XML trees in Visual Basic](./create-xml-trees-visual-basic.md). +- You can parse XML from various sources, including a , text files, or a Web address (URL). For more information, see [Parse XML](./parse-string.md). +- You can use an to populate the tree. For more information, see . +- If you have a module that can write content to an , you can use the method to create a writer, pass the writer to the module, and then use the content that is written to the to populate the XML tree. + +The following example creates a tree. The C# version uses nested element creations. You can use the same technique in Visual Basic, but this example uses XML literals. + +```csharp +XElement contacts = + new XElement("Contacts", + new XElement("Contact", + new XElement("Name", "Patrick Hines"), + new XElement("Phone", "206-555-0144"), + new XElement("Address", + new XElement("Street1", "123 Main St"), + new XElement("City", "Mercer Island"), + new XElement("State", "WA"), + new XElement("Postal", "68042") + ) + ) + ); +``` + +```vb +Dim contacts As XElement = _ + + + Patrick Hines + 206-555-0144 +
+ 123 Main St + Mercer Island + WA + 68042 +
+
+
+``` + +You can also use a LINQ to XML query to populate an XML tree, as shown in the following example: + +```csharp +XElement srcTree = new XElement("Root", + new XElement("Element", 1), + new XElement("Element", 2), + new XElement("Element", 3), + new XElement("Element", 4), + new XElement("Element", 5) +); +XElement xmlTree = new XElement("Root", + new XElement("Child", 1), + new XElement("Child", 2), + from el in srcTree.Elements() + where (int)el > 2 + select el +); +Console.WriteLine(xmlTree); +``` + +```vb +Dim srcTree As XElement = _ + + 1 + 2 + 3 + 4 + 5 + +Dim xmlTree As XElement = _ + + 1 + 2 + <%= From el In srcTree.Elements() _ + Where el.Value > 2 _ + Select el %> + +Console.WriteLine(xmlTree) +``` + +The example produces this output: + +```xml + + 1 + 2 + 3 + 4 + 5 + +``` + +## Serialize XML trees + +You can serialize the XML tree to a , a , or an . + +For more information, see [Serialize XML trees](./preserve-white-space-serializing.md). + +## Retrieve XML data via axis methods + +You can use axis methods to retrieve attributes, child elements, descendant elements, and ancestor elements. LINQ to XML queries operate on axis methods, and provide several flexible and powerful ways to navigate through and process an XML tree. + +For more information, see [LINQ to XML axes overview](./linq-xml-axes-overview.md). + +## Query XML trees + +You can write LINQ to XML queries that extract data from an XML tree. + +For more information, see [Query XML trees overview](./query-xml-trees-overview.md). + +## Modify XML trees + +You can modify an element in a variety of ways, including changing its content or attributes. You can also remove an element from its parent. + +For more information, see [Modify XML trees](./in-memory-xml-tree-modification-vs-functional-construction.md). + +## See also + +- [LINQ to XML programming overview](functional-vs-procedural-programming.md) diff --git a/docs/visual-basic/programming-guide/concepts/linq/xattribute-class-overview.md b/docs/visual-basic/programming-guide/concepts/linq/xattribute-class-overview.md deleted file mode 100644 index a736d36e3cbb5..0000000000000 --- a/docs/visual-basic/programming-guide/concepts/linq/xattribute-class-overview.md +++ /dev/null @@ -1,73 +0,0 @@ ---- -title: "XAttribute Class Overview" -ms.date: 07/20/2015 -ms.assetid: 7781580a-9583-4a1b-ae1e-91c5936eb0b1 ---- -# XAttribute Class Overview (Visual Basic) -Attributes are name/value pairs that are associated with an element. The class represents XML attributes. - -## Overview - Working with attributes in [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is similar to working with elements. Their constructors are similar. The methods that you use to retrieve collections of them are similar. A LINQ query expression for a collection of attributes looks very similar to a LINQ query expression for a collection of elements. - - The order in which attributes were added to an element is preserved. That is, when you iterate through the attributes, you see them in the same order that they were added. - -## The XAttribute Constructor - The following constructor of the class is the one that you will most commonly use: - -|Constructor|Description| -|-----------------|-----------------| -|`XAttribute(XName name, object content)`|Creates an object. The `name` argument specifies the name of the attribute; `content` specifies the content of the attribute.| - -### Creating an Element with an Attribute - The following code shows an element that contains an attribute using XML literals in Visual Basic: - -```vb -Dim phone As XElement = 555-555-5555 -Console.WriteLine(phone) -``` - - This example produces the following output: - -```xml -555-555-5555 -``` - -### Functional Construction of Attributes - You can construct objects in-line with the construction of objects, as follows: - -```vb -Dim c As XElement = _ - - - John Doe - - 555-555-5555 - 666-666-6666 - - - -Console.WriteLine(c) -``` - - This example produces the following output: - -```xml - - - John Doe - - 555-555-5555 - 666-666-6666 - - - -``` - -### Attributes Are Not Nodes - There are some differences between attributes and elements. objects are not nodes in the XML tree. They are name/value pairs associated with an XML element. In contrast to the Document Object Model (DOM), this more closely reflects the structure of XML. Although objects are not actually nodes in the XML tree, working with objects is very similar to working with objects. - - This distinction is primarily important only to developers who are writing code that works with XML trees at the node level. Many developers will not be concerned with this distinction. - -## See also - -- [LINQ to XML Programming Overview (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/linq-to-xml-programming-overview.md) diff --git a/docs/visual-basic/programming-guide/concepts/linq/xelement-class-overview.md b/docs/visual-basic/programming-guide/concepts/linq/xelement-class-overview.md deleted file mode 100644 index a2d9e086d72a9..0000000000000 --- a/docs/visual-basic/programming-guide/concepts/linq/xelement-class-overview.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: "XElement Class Overview" -ms.date: 07/20/2015 -ms.assetid: 52331fcd-6023-4d19-b423-7b24f2d86ded ---- -# XElement Class Overview (Visual Basic) -The class is one of the fundamental classes in [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)]. It represents an XML element. You can use this class to create elements; change the content of the element; add, change, or delete child elements; add attributes to an element; or serialize the contents of an element in text form. You can also interoperate with other classes in , such as , , and . - -## XElement Functionality - This topic describes the functionality provided by the class. - -### Constructing XML Trees - You can construct XML trees in a variety of ways, including the following: - -- You can construct an XML tree in code. For more information, see [Creating XML Trees (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/creating-xml-trees.md). - -- You can parse XML from various sources, including a , text files, or a Web address (URL). For more information, see [Parsing XML (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/parsing-xml.md). - -- You can use an to populate the tree. For more information, see . - -- If you have a module that can write content to an , you can use the method to create a writer, pass the writer to the module, and then use the content that is written to the to populate the XML tree. - - However, the most common way to create an XML tree is as follows: - -```vb -Dim contacts As XElement = _ - - - Patrick Hines - 206-555-0144 -
- 123 Main St - Mercer Island - WA - 68042 -
-
-
-``` - - Another very common technique for creating an XML tree involves using the results of a LINQ query to populate an XML tree, as shown in the following example: - -```vb -Dim srcTree As XElement = _ - - 1 - 2 - 3 - 4 - 5 - -Dim xmlTree As XElement = _ - - 1 - 2 - <%= From el In srcTree.Elements() _ - Where el.Value > 2 _ - Select el %> - -Console.WriteLine(xmlTree) -``` - - This example produces the following output: - -```xml - - 1 - 2 - 3 - 4 - 5 - -``` - -### Serializing XML Trees - You can serialize the XML tree to a , a , or an . - - For more information, see [Serializing XML Trees (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/serializing-xml-trees.md). - -### Retrieving XML Data via Axis Methods - You can use axis methods to retrieve attributes, child elements, descendant elements, and ancestor elements. LINQ queries operate on axis methods, and provide several flexible and powerful ways to navigate through and process an XML tree. - - For more information, see [LINQ to XML Axes (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/linq-to-xml-axes.md). - -### Querying XML Trees - You can write LINQ queries that extract data from an XML tree. - - For more information, see [Querying XML Trees (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/querying-xml-trees.md). - -### Modifying XML Trees - You can modify an element in a variety of ways, including changing its content or attributes. You can also remove an element from its parent. - - For more information, see [Modifying XML Trees (LINQ to XML) (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/modifying-xml-trees-linq-to-xml.md). - -## See also - -- [LINQ to XML Programming Overview (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/linq-to-xml-programming-overview.md) From 9741f6fb2e8c094b8c15631be5f9a14eaf9c4630 Mon Sep 17 00:00:00 2001 From: Maira Wenzel Date: Sun, 2 Feb 2020 11:20:17 -0800 Subject: [PATCH 2/2] fixes + acrolynx --- .../linq/xattribute-class-overview.md | 105 +++++++++++++ docs/standard/linq/xelement-class-overview.md | 145 ++++++++++++++++++ docs/standard/xattribute-class-overview.md | 105 ------------- docs/standard/xelement-class-overview.md | 137 ----------------- 4 files changed, 250 insertions(+), 242 deletions(-) create mode 100644 docs/standard/linq/xattribute-class-overview.md create mode 100644 docs/standard/linq/xelement-class-overview.md delete mode 100644 docs/standard/xattribute-class-overview.md delete mode 100644 docs/standard/xelement-class-overview.md diff --git a/docs/standard/linq/xattribute-class-overview.md b/docs/standard/linq/xattribute-class-overview.md new file mode 100644 index 0000000000000..bdb305d8c4dd4 --- /dev/null +++ b/docs/standard/linq/xattribute-class-overview.md @@ -0,0 +1,105 @@ +--- +title: "XAttribute class overview" +ms.date: 07/20/2015 +dev_langs: + - "csharp" + - "vb" +ms.assetid: 5a630f24-f9ad-400e-831e-c14ebfc9e142 +--- + +# XAttribute class overview + +Attributes are name/value pairs that are associated with an element. The class represents XML attributes. + +Working with attributes in LINQ to XML is similar to working with elements. Their constructors are similar. The methods that you use to retrieve collections of them are similar. A LINQ query expression for a collection of attributes looks similar to a LINQ query expression for a collection of elements. + +The order in which attributes were added to an element is preserved. That is, when you iterate through the attributes, you see them in the same order that they were added. + +## The XAttribute constructor + +The following constructor of the class is the one that you'll most commonly use: + +|Constructor|Description| +|-----------------|-----------------| +|`XAttribute(XName name, object content)`|Creates an object. The `name` argument specifies the name of the attribute; `content` specifies the content of the attribute.| + +## Example: Create an element with an attribute + +The following example shows the common task of creating an element that contains an attribute. + +```csharp +XElement phone = new XElement("Phone", + new XAttribute("Type", "Home"), + "555-555-5555"); +Console.WriteLine(phone); +``` + +```vb +Dim phone As XElement = 555-555-5555 +Console.WriteLine(phone) +``` + +The example produces this output: + +```xml +555-555-5555 +``` + +## Example: Functional construction of attributes + +You can construct objects in-line with the construction of objects, as shown in the following example: + +```csharp +XElement c = new XElement("Customers", + new XElement("Customer", + new XElement("Name", "John Doe"), + new XElement("PhoneNumbers", + new XElement("Phone", + new XAttribute("type", "home"), + "555-555-5555"), + new XElement("Phone", + new XAttribute("type", "work"), + "666-666-6666") + ) + ) +); +Console.WriteLine(c); +``` + +```vb +Dim c As XElement = _ + + + John Doe + + 555-555-5555 + 666-666-6666 + + + +Console.WriteLine(c) +``` + +The example produces this output: + +```xml + + + John Doe + + 555-555-5555 + 666-666-6666 + + + +``` + +## Attributes aren't nodes + +There are some differences between attributes and elements. objects aren't nodes in the XML tree. They're name/value pairs associated with an XML element. In contrast to the Document Object Model (DOM), this more closely reflects the structure of XML. Although objects aren't actually nodes in the XML tree, working with objects is similar to working with objects. + +This distinction is primarily important only to developers who are writing code that works with XML trees at the node level. Many developers won't be concerned with this distinction. + +## See also + +- [LINQ to XML overview](linq-xml-overview.md) diff --git a/docs/standard/linq/xelement-class-overview.md b/docs/standard/linq/xelement-class-overview.md new file mode 100644 index 0000000000000..403c207cc5c34 --- /dev/null +++ b/docs/standard/linq/xelement-class-overview.md @@ -0,0 +1,145 @@ +--- +title: "XElement class overview" +ms.date: 07/20/2015 +dev_langs: + - "csharp" + - "vb" +ms.assetid: 2b9f0cd8-a1d1-4037-accf-0f38a410fa11 +--- + +# XElement class overview + +The class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: + +- Create elements. +- Change the content of the element. +- Add, change, or delete child elements. +- Add attributes to an element. +- Serialize the contents of an element in text form. + +You can also interoperate with other classes in , such as , , and . + +This article describes the functionality provided by the class. + +## Construct XML trees + +You can construct XML trees in different ways, including the following: + +- You can construct an XML tree in code. For more information, see [XML trees](functional-construction.md). +- You can parse XML from various sources, including a , text files, or a Web address (URL). For more information, see [Parse XML](parse-string.md). +- You can use an to populate the tree. For more information, see . +- If you have a module that can write content to an , you can use the method to create a writer, pass the writer to the module, and then use the content that is written to the to populate the XML tree. + +The following example creates a tree. The C# version uses nested element creations. You can use the same technique in Visual Basic, but this example uses XML literals. + +```csharp +XElement contacts = + new XElement("Contacts", + new XElement("Contact", + new XElement("Name", "Patrick Hines"), + new XElement("Phone", "206-555-0144"), + new XElement("Address", + new XElement("Street1", "123 Main St"), + new XElement("City", "Mercer Island"), + new XElement("State", "WA"), + new XElement("Postal", "68042") + ) + ) + ); +``` + +```vb +Dim contacts As XElement = _ + + + Patrick Hines + 206-555-0144 +
+ 123 Main St + Mercer Island + WA + 68042 +
+
+
+``` + +You can also use a LINQ to XML query to populate an XML tree, as shown in the following example: + +```csharp +XElement srcTree = new XElement("Root", + new XElement("Element", 1), + new XElement("Element", 2), + new XElement("Element", 3), + new XElement("Element", 4), + new XElement("Element", 5) +); +XElement xmlTree = new XElement("Root", + new XElement("Child", 1), + new XElement("Child", 2), + from el in srcTree.Elements() + where (int)el > 2 + select el +); +Console.WriteLine(xmlTree); +``` + +```vb +Dim srcTree As XElement = _ + + 1 + 2 + 3 + 4 + 5 + +Dim xmlTree As XElement = _ + + 1 + 2 + <%= From el In srcTree.Elements() _ + Where el.Value > 2 _ + Select el %> + +Console.WriteLine(xmlTree) +``` + +The example produces this output: + +```xml + + 1 + 2 + 3 + 4 + 5 + +``` + +## Serialize XML trees + +You can serialize the XML tree to a , a , or an . + +For more information, see [Serialize XML trees](preserve-white-space-serializing.md). + +## Retrieve XML data via axis methods + +You can use axis methods to retrieve attributes, child elements, descendant elements, and ancestor elements. LINQ to XML queries operate on axis methods, and provide several flexible and powerful ways to navigate through and process an XML tree. + +For more information, see [LINQ to XML axes overview](linq-xml-axes-overview.md). + +## Query XML trees + +You can write LINQ to XML queries that extract data from an XML tree. + +For more information, see [Query XML trees overview](query-xml-trees-overview.md). + +## Modify XML trees + +You can modify an element in different ways, including changing its content or attributes. You can also remove an element from its parent. + +For more information, see [Modify XML trees](in-memory-xml-tree-modification-vs-functional-construction.md). + +## See also + +- [LINQ to XML programming overview](functional-vs-procedural-programming.md) diff --git a/docs/standard/xattribute-class-overview.md b/docs/standard/xattribute-class-overview.md deleted file mode 100644 index f3642715731b5..0000000000000 --- a/docs/standard/xattribute-class-overview.md +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: "XAttribute class overview" -ms.date: 07/20/2015 -dev_langs: - - "csharp" - - "vb" -ms.assetid: 5a630f24-f9ad-400e-831e-c14ebfc9e142 ---- - -# XAttribute class overview (C# and Visual Basic) - -Attributes are name/value pairs that are associated with an element. The class represents XML attributes. - -Working with attributes in LINQ to XML is similar to working with elements. Their constructors are similar. The methods that you use to retrieve collections of them are similar. A LINQ query expression for a collection of attributes looks very similar to a LINQ query expression for a collection of elements. - -The order in which attributes were added to an element is preserved. That is, when you iterate through the attributes, you see them in the same order that they were added. - -## The XAttribute constructor - -The following constructor of the class is the one that you will most commonly use: - -|Constructor|Description| -|-----------------|-----------------| -|`XAttribute(XName name, object content)`|Creates an object. The `name` argument specifies the name of the attribute; `content` specifies the content of the attribute.| - -## Example: Create an element with an attribute - -The following example shows the common task of creating an element that contains an attribute. - -```csharp -XElement phone = new XElement("Phone", - new XAttribute("Type", "Home"), - "555-555-5555"); -Console.WriteLine(phone); -``` - -```vb -Dim phone As XElement = 555-555-5555 -Console.WriteLine(phone) -``` - -The example produces this output: - -```xml -555-555-5555 -``` - -## Example: Functional construction of attributes - -You can construct objects in-line with the construction of objects. An example of code that does this is: - -```csharp -XElement c = new XElement("Customers", - new XElement("Customer", - new XElement("Name", "John Doe"), - new XElement("PhoneNumbers", - new XElement("Phone", - new XAttribute("type", "home"), - "555-555-5555"), - new XElement("Phone", - new XAttribute("type", "work"), - "666-666-6666") - ) - ) -); -Console.WriteLine(c); -``` - -```vb -Dim c As XElement = _ - - - John Doe - - 555-555-5555 - 666-666-6666 - - - -Console.WriteLine(c) -``` - -The example produces this output: - -```xml - - - John Doe - - 555-555-5555 - 666-666-6666 - - - -``` - -## Attributes are not nodes - -There are some differences between attributes and elements. objects are not nodes in the XML tree. They are name/value pairs associated with an XML element. In contrast to the Document Object Model (DOM), this more closely reflects the structure of XML. Although objects are not actually nodes in the XML tree, working with objects is very similar to working with objects. - -This distinction is primarily important only to developers who are writing code that works with XML trees at the node level. Many developers will not be concerned with this distinction. - -## See also - -- [LINQ to XML overview](./linq-xml-overview.md) diff --git a/docs/standard/xelement-class-overview.md b/docs/standard/xelement-class-overview.md deleted file mode 100644 index 4e2b99168d10b..0000000000000 --- a/docs/standard/xelement-class-overview.md +++ /dev/null @@ -1,137 +0,0 @@ ---- -title: "XElement class overview" -ms.date: 07/20/2015 -dev_langs: - - "csharp" - - "vb" -ms.assetid: 2b9f0cd8-a1d1-4037-accf-0f38a410fa11 ---- - -# XElement class overview (C# and Visual Basic) - -The class is one of the fundamental classes in LINQ to XML. It represents an XML element. You can use this class to create elements; change the content of the element; add, change, or delete child elements; add attributes to an element; or serialize the contents of an element in text form. You can also interoperate with other classes in , such as , , and . - -This article describes the functionality provided by the class. - -## Construct XML trees - -You can construct XML trees in a variety of ways, including the following: - -- You can construct an XML tree in code. For more information, see [Create XML trees in C#](./create-xml-trees-csharp.md) and [Create XML trees in Visual Basic](./create-xml-trees-visual-basic.md). -- You can parse XML from various sources, including a , text files, or a Web address (URL). For more information, see [Parse XML](./parse-string.md). -- You can use an to populate the tree. For more information, see . -- If you have a module that can write content to an , you can use the method to create a writer, pass the writer to the module, and then use the content that is written to the to populate the XML tree. - -The following example creates a tree. The C# version uses nested element creations. You can use the same technique in Visual Basic, but this example uses XML literals. - -```csharp -XElement contacts = - new XElement("Contacts", - new XElement("Contact", - new XElement("Name", "Patrick Hines"), - new XElement("Phone", "206-555-0144"), - new XElement("Address", - new XElement("Street1", "123 Main St"), - new XElement("City", "Mercer Island"), - new XElement("State", "WA"), - new XElement("Postal", "68042") - ) - ) - ); -``` - -```vb -Dim contacts As XElement = _ - - - Patrick Hines - 206-555-0144 -
- 123 Main St - Mercer Island - WA - 68042 -
-
-
-``` - -You can also use a LINQ to XML query to populate an XML tree, as shown in the following example: - -```csharp -XElement srcTree = new XElement("Root", - new XElement("Element", 1), - new XElement("Element", 2), - new XElement("Element", 3), - new XElement("Element", 4), - new XElement("Element", 5) -); -XElement xmlTree = new XElement("Root", - new XElement("Child", 1), - new XElement("Child", 2), - from el in srcTree.Elements() - where (int)el > 2 - select el -); -Console.WriteLine(xmlTree); -``` - -```vb -Dim srcTree As XElement = _ - - 1 - 2 - 3 - 4 - 5 - -Dim xmlTree As XElement = _ - - 1 - 2 - <%= From el In srcTree.Elements() _ - Where el.Value > 2 _ - Select el %> - -Console.WriteLine(xmlTree) -``` - -The example produces this output: - -```xml - - 1 - 2 - 3 - 4 - 5 - -``` - -## Serialize XML trees - -You can serialize the XML tree to a , a , or an . - -For more information, see [Serialize XML trees](./preserve-white-space-serializing.md). - -## Retrieve XML data via axis methods - -You can use axis methods to retrieve attributes, child elements, descendant elements, and ancestor elements. LINQ to XML queries operate on axis methods, and provide several flexible and powerful ways to navigate through and process an XML tree. - -For more information, see [LINQ to XML axes overview](./linq-xml-axes-overview.md). - -## Query XML trees - -You can write LINQ to XML queries that extract data from an XML tree. - -For more information, see [Query XML trees overview](./query-xml-trees-overview.md). - -## Modify XML trees - -You can modify an element in a variety of ways, including changing its content or attributes. You can also remove an element from its parent. - -For more information, see [Modify XML trees](./in-memory-xml-tree-modification-vs-functional-construction.md). - -## See also - -- [LINQ to XML programming overview](functional-vs-procedural-programming.md)