Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,26 @@
"redirect_url": "/dotnet/standard/linq/build-linq-xml-examples",
"redirect_document_id": true
},
{
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-catch-parsing-errors.md",
"redirect_url": "/dotnet/standard/linq/catch-parsing-errors",
"redirect_document_id": true
},
{
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-create-a-tree-from-an-xmlreader.md",
"redirect_url": "/dotnet/standard/linq/create-tree-xmlreader",
"redirect_document_id": true
},
{
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-load-xml-from-a-file.md",
"redirect_url": "/dotnet/standard/linq/load-xml-file",
"redirect_document_id": true
},
{
"source_path": "docs/csharp/programming-guide/concepts/linq/preserving-white-space-while-loading-or-parsing-xml1.md",
"redirect_url": "/dotnet/standard/linq/preserve-white-space-loading-parsing-xml",
"redirect_document_id": true
},
{
"source_path": "docs/csharp/programming-guide/concepts/linq/creating-xml-trees-linq-to-xml-2.md",
"redirect_url": "/dotnet/standard/linq/create-xml-trees",
Expand Down Expand Up @@ -4280,6 +4300,26 @@
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-build-linq-to-xml-examples.md",
"redirect_url": "/dotnet/standard/linq/build-linq-xml-examples"
},
{
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-catch-parsing-errors.md",
"redirect_url": "/dotnet/standard/linq/catch-parsing-errors",
"redirect_document_id": false
},
{
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-create-a-tree-from-an-xmlreader.md",
"redirect_url": "/dotnet/standard/linq/create-tree-xmlreader",
"redirect_document_id": false
},
{
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-load-xml-from-a-file.md",
"redirect_url": "/dotnet/standard/linq/load-xml-file",
"redirect_document_id": false
},
{
"source_path": "docs/visual-basic/programming-guide/concepts/linq/preserving-white-space-while-loading-or-parsing-xml.md",
"redirect_url": "/dotnet/standard/linq/preserve-white-space-loading-parsing-xml",
"redirect_document_id": false
},
{
"source_path": "docs/visual-basic/programming-guide/concepts/linq/functional-construction-linq-to-xml.md",
"redirect_url": "/dotnet/standard/linq/functional-construction"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

62 changes: 62 additions & 0 deletions docs/standard/linq/catch-parsing-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: "How to catch parsing errors - LINQ to XML"
description: Learn how to detect badly formed or invalid XML.
ms.date: 7/20/2015
dev_langs:
- "csharp"
- "vb"
ms.assetid: bfb612d4-5605-48ef-8c93-915cf9d5dcfb
---

# How to catch parsing errors (LINQ to XML)

This article shows how to detect badly formed or invalid XML in C# or Visual Basic.

LINQ to XML is implemented using <xref:System.Xml.XmlReader>. If badly formed or invalid XML is passed to LINQ to XML, the underlying <xref:System.Xml.XmlReader> class will throw an exception. The various methods that parse XML, such as <xref:System.Xml.Linq.XElement.Parse%2A?displayProperty=nameWithType>, do not catch the exception; the exception can then be caught by your application.

## Example: Parse invalid XML

The following code tries to parse invalid XML.

```csharp
try {
XElement contacts = XElement.Parse(
@"<Contacts>
<Contact>
<Name>Jim Wilson</Name>
</Contact>
</Contcts>");

Console.WriteLine(contacts);
}
catch (System.Xml.XmlException e)
{
Console.WriteLine(e.Message);
}
```

```vb
Try
Dim contacts As XElement = XElement.Parse("<Contacts>" & vbCrLf & _
" <Contact>" & vbCrLf & _
" <Name>Jim Wilson</Name>" & vbCrLf & _
" </Contact>" & vbCrLf & _
"</Contcts>")

Console.WriteLine(contacts)
Catch e As System.Xml.XmlException
Console.WriteLine(e.Message)
End Try
```

Because of the invalid end tag `</Contcts>`, the example throws the following exception:

```console
The 'Contacts' start tag on line 1 doesn't match the end tag of 'Contcts'. Line 5, position 13.
```

For information about the exceptions that the <xref:System.Xml.Linq.XElement.Parse%2A?displayProperty=nameWithType>, <xref:System.Xml.Linq.XDocument.Parse%2A?displayProperty=nameWithType>, <xref:System.Xml.Linq.XElement.Load%2A?displayProperty=nameWithType>, and <xref:System.Xml.Linq.XDocument.Load%2A?displayProperty=nameWithType> methods throw, see the <xref:System.Xml.XmlReader> documentation.

## See also

- [How to parse a string (LINQ to XML)](parse-string.md)
66 changes: 66 additions & 0 deletions docs/standard/linq/create-tree-xmlreader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: "How to create a tree from an XmlReader - LINQ to XML"
description: Learn how to create an XML tree directly from an XmlReader.
ms.date: 07/20/2015
dev_langs:
- "csharp"
- "vb"
ms.assetid: 60951c9c-7087-406c-b5bb-c60e58609b21
---

# How to create a tree from an XmlReader (LINQ to XML)

This article shows how to create an XML tree directly from an <xref:System.Xml.XmlReader> in C# or Visual Basic. To create an <xref:System.Xml.Linq.XElement> from an <xref:System.Xml.XmlReader>, you position the <xref:System.Xml.XmlReader> on an element node. The <xref:System.Xml.XmlReader> will skip comments and processing instructions, but if the <xref:System.Xml.XmlReader> is positioned on a text node, an error will be thrown. To avoid such errors, position the <xref:System.Xml.XmlReader> on an element before you create an XML tree from the <xref:System.Xml.XmlReader>.

## Example: Load XElement object from an XmlReader object

This example uses the XML document [Sample XML file: Books (LINQ to XML)](sample-xml-file-books.md).

The following code creates a <xref:System.Xml.XmlReader> object, reads nodes until it finds the first element node, and loads the <xref:System.Xml.Linq.XElement> object.

```csharp
XmlReader r = XmlReader.Create("books.xml");
while (r.NodeType != XmlNodeType.Element)
r.Read();
XElement e = XElement.Load(r);
Console.WriteLine(e);
```

```vb
Dim r As XmlReader = XmlReader.Create("books.xml")
Do While r.NodeType <> XmlNodeType.Element
r.Read()
Loop
Dim e As XElement = XElement.Load(r)
Console.WriteLine(e)
```

The example produces this output:

```xml
<Catalog>
<Book id="bk101">
<Author>Garghentini, Davide</Author>
<Title>XML Developer's Guide</Title>
<Genre>Computer</Genre>
<Price>44.95</Price>
<PublishDate>2000-10-01</PublishDate>
<Description>An in-depth look at creating applications
with XML.</Description>
</Book>
<Book id="bk102">
<Author>Garcia, Debra</Author>
<Title>Midnight Rain</Title>
<Genre>Fantasy</Genre>
<Price>5.95</Price>
<PublishDate>2000-12-16</PublishDate>
<Description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</Description>
</Book>
</Catalog>
```

## See also

- [Parse XML](parse-string.md)
4 changes: 2 additions & 2 deletions docs/standard/linq/functional-construction.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ The example produces this output:

## See also

- [Create XML Trees in C#](create-xml-trees.md)
- [XML literals in Visual Basic](xml-literals.md)
- [Create XML Trees in C# (LINQ to XML)](create-xml-trees.md)
- [XML literals in Visual Basic (LINQ to XML)](xml-literals.md)
2 changes: 1 addition & 1 deletion docs/standard/linq/linq-xml-vs-dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Another problem with the DOM is that it doesn't let you change the name of a nod

## Static method support for loading XML

LINQ to XML lets you load XML by using static methods, instead of instance methods. This simplifies loading and parsing. For more information, see [How to load XML from a file](load-xml-file.md).
LINQ to XML lets you load XML by using static methods, instead of instance methods. This simplifies loading and parsing. For more information, see [How to load XML from a file (LINQ to XML)](load-xml-file.md).

## Removal of support for DTD constructs

Expand Down
Loading