dotnet / dotnet/dotnet-api-docs

example: open input XML file with Open/Read instead of OpenOrCreate

Open
#1,042 0 comments 0 reactions 0 assignees View on GitHub
area-Serialization dotnet-api/prod Pri2 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

In the de-serialization example, open the input file with `FileMode.Open, FileAccess.Read` instead of `FileMode.OpenOrCreate`

```C#
// Deserialize an instance of the Person class
// from an XML file. First create an instance of the
// XmlDictionaryReader.
var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
var reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
```

If you use `OpenOrCreate` and the file does not exist, a confusing exception will be raised: it is better practice to have the correct exception (file not found) thrown at the earliest opportunity.

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: 0f4ff2d5-3f9f-4b6d-4e8f-9d026a83e9cf
* Version Independent ID: f15142ca-da1d-387f-94ba-b95f8ab835ab
* Content: [DataContractAttribute Class (System.Runtime.Serialization)](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?view=netframework-4.7.2)
* Content Source: [xml/System.Runtime.Serialization/DataContractAttribute.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Runtime.Serialization/DataContractAttribute.xml)
* Product: **dotnet-api**
* GitHub Login: @dotnet-bot
* Microsoft Alias: **dotnetcontent**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.