{"id":104103,"date":"2024-01-29T13:47:32","date_gmt":"2024-01-29T12:47:32","guid":{"rendered":"https:\/\/code-maze.com\/?p=104103"},"modified":"2024-01-31T15:51:18","modified_gmt":"2024-01-31T14:51:18","slug":"csharp-reading-xml-documents","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/","title":{"rendered":"How to Read XML Documents in C#"},"content":{"rendered":"<p>In this article, we&#8217;ll talk about how to read XML Documents in C#. In the <a href=\"https:\/\/code-maze.com\/csharp-how-to-create-xml-documents\" target=\"_blank\" rel=\"noopener\">preceding article<\/a>, we addressed the creation of custom XML documents.<\/p>\n<p>Also, we have already explored how to serialize and deserialize objects to and from XML in the articles titled <a href=\"https:\/\/code-maze.com\/csharp-xml-serialization\/\" target=\"_blank\" rel=\"noopener\">Serializing Objects to XML in C#<\/a> and\u00a0<a href=\"https:\/\/code-maze.com\/csharp-xml-deserialization\/\" target=\"_blank\" rel=\"noopener\">XML Deserialization in C#<\/a>. So what else can we learn?<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/xml-csharp\/ReadXML\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<h2>Reading XML Documents<\/h2>\n<p><em>NOTE: The code will employ the classes <code>Person<\/code>, <code>People<\/code>, and <code>CreateXMLUsingXmlWriter<\/code>, developed in the <a href=\"https:\/\/code-maze.com\/csharp-how-to-create-xml-documents\" target=\"_blank\" rel=\"noopener\">preceding article<\/a>, and we won&#8217;t duplicate the code here.<\/em><\/p>\n<p>Similar to creating XML documents, where we could choose between <code>LinqToXml<\/code> or <code>XmlWriter<\/code>, we once again have two potential approaches: utilizing the <code>XDocument<\/code> class or opting for the older <code>XmlReader<\/code> class.<\/p>\n<p>The <code>XDocument<\/code> path is simpler and more user-friendly, while <code>XmlReader<\/code> provides greater control over the reading process.<\/p>\n<h2>Using XDocument to Read XML Documents in C#<\/h2>\n<p><strong>Reading XML using <\/strong><code>XDocument<\/code><strong> couldn&#8217;t be simpler.<\/strong> Whether we are reading XML from a file or a string, the <code>XDocument<\/code> class provides functions for these scenarios.<\/p>\n<p>To read an XML document <strong>from a string, we use the<\/strong> <code>Parse()<\/code><strong> method<\/strong>. To read an XML <strong>from a file, we use the <\/strong><code>Load()<\/code><strong> method<\/strong>.<\/p>\n<p>If the document is not syntactically correct, these methods will throw an exception:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class ReadingXmlUsingXDocument\r\n{\r\n    public XDocument ReadXmlAndCatchErrors(string xml)\r\n    {\r\n        try\r\n        {\r\n            return XDocument.Parse(xml);\r\n        }\r\n        catch (Exception ex)\r\n        {\r\n            Console.WriteLine(\"Something went wrong:\");\r\n            Console.WriteLine(ex);\r\n        }\r\n\r\n        return new XDocument();\r\n    }\r\n}<\/pre>\n<p>We&#8217;ve created a <code>ReadingXmlUsingXDocument<\/code> class utilizing the <code>XDocument<\/code> class. This class features a method, <code>ReadXmlAndCatchErrors()<\/code>, which parses the given XML string. If any errors occur during parsing, the method catches the error, displays it in the console, and returns an empty XDocument.<\/p>\n<p>To evaluate this method, we can generate XML documents using the previously developed <code>CreateXMLUsingXmlWriter<\/code> class. First, we create a valid XML document and then proceed to read it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string TestValidXml()\r\n{\r\n    var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateSimpleXML(People.GetOne()));\r\n\r\n    return xmlDoc.ToString();\r\n}<\/pre>\n<p>Everything unfolds as anticipated. <strong>The document is successfully created, and we can both read its content and output it to the console.<\/strong><\/p>\n<p>But what happens if we provide invalid XML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string TestInvalidXml()\r\n{\r\n    var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateWrongXML(People.GetOne()));\r\n\r\n    return xmlDoc.ToString();\r\n}<\/pre>\n<p><code>XDocument.Parse()<\/code> <strong>will throw an exception and our<\/strong> <code>ReadXmlAndCatchErrors()<\/code> <strong>method catches it and returns an empty XDocument.<\/strong><\/p>\n<h2>Getting Values From XDocument<\/h2>\n<p>With the XML document now within the <code>XDocument<\/code> object, the question arises: <strong>How do we extract the name or age of a person from the XDocument object?<\/strong><\/p>\n<p>As this extends beyond the scope of this article, we will briefly outline a few options using the initially created &#8216;people document&#8217;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;person&gt;\r\n  &lt;name&gt;\r\n    &lt;firstName&gt;Emma&lt;\/firstName&gt;\r\n    &lt;lastName&gt;Brown&lt;\/lastName&gt;\r\n  &lt;\/name&gt;\r\n  &lt;email&gt;emma.brown@code-maze.com&lt;\/email&gt;\r\n  &lt;age&gt;58&lt;\/age&gt;\r\n&lt;\/person&gt;<\/pre>\n<p>XML document contains data about a person. Among them are the email and the age of the person we want to extract.<\/p>\n<h3>Reading With the Use of the Element Collection<\/h3>\n<p><strong>Every element inside <code>XDocument<\/code> has an element collection of all its children<\/strong>. If an element has no children, this collection will be empty.<\/p>\n<p>We traverse through the XML tree by referencing the elements by name and then read their values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string TestReadWithElementCollection()\r\n{\r\n    var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateSimpleXML(People.GetOne()));\r\n    var name = xmlDoc.Root!.Element(\"name\")!.Element(\"firstName\")!.Value;\r\n    var age = xmlDoc.Root!.Element(\"age\")!.Value;\r\n\r\n    return $\"Name: {name}, Age: {age}\";\r\n}<\/pre>\n<p>We start from the root of the document, which is a <code>&lt;person&gt;<\/code> element. There we search for the <code>&lt;name&gt;<\/code> element, and inside the <code>&lt;name&gt;<\/code> element, we find the <code>&lt;firstName&gt;<\/code> element. From the <code>&lt;firstName&gt;<\/code> element, we take the value, which is, of course, the name of the person.<\/p>\n<p>The same is true for the age.<\/p>\n<p><em>Notice that here we are using the &#8216;!&#8217; operator, as we know in our example case, that the XML document contains all the elements we are searching for.<\/em><\/p>\n<h3>Using XPath<\/h3>\n<p>To retrieve data from an XML document, we can also utilize <code>XPath<\/code>. <strong>The <\/strong><code>XPath<\/code><strong> language is quite extensive, but in this article, we will provide a simple example of reading the name and age fields:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string TestReadUsingXPath()\r\n{\r\n    var xmlDoc = ReadXmlAndCatchErrors(CreateXMLUsingXmlWriter.CreateSimpleXML(People.GetOne()));\r\n\r\n    var name = xmlDoc.XPathSelectElement(\"\/person\/name\/firstName\")!.Value;\r\n    var age = xmlDoc.XPathSelectElement(\"\/person\/age\")!.Value;\r\n\r\n    return $\"Name: {name}, Age: {age}\";\r\n}<\/pre>\n<p><strong>In this example, we use <\/strong><code>XPath<\/code><strong> to locate elements like navigating a file system.<\/strong> For instance, to access the <code>&lt;firstName&gt;<\/code> element, we use the path &#8220;<code>\/person\/name\/firstName<\/code>.&#8221;<\/p>\n<p>There are various ways to express <code>XPath<\/code> queries, such as:<\/p>\n<ul>\n<li>\n<p><code>\/\/firstName<\/code><\/p>\n<\/li>\n<li>\n<p><code>\/person\/name\/*[1]<\/code><\/p>\n<\/li>\n<li>\n<p><code>\/person\/name\/*[local-name()='firstName']<\/code><\/p>\n<\/li>\n<\/ul>\n<p>\nThe first <code>XPath<\/code> expression<strong> selects all<\/strong> <code>&lt;firstName&gt;<\/code> elements from anywhere in the XML document. The second selects the <strong>first child element under<\/strong> the <code>&lt;name&gt;<\/code> element <strong>within<\/strong> the <code>&lt;person&gt;<\/code> element. The last one selects<strong> the first child element under<\/strong> the <code>&lt;name&gt;<\/code> element <strong>within<\/strong> the <code>&lt;person&gt;<\/code> element, <strong>specifically if its local name is <code>&lt;firstName&gt;<\/code>, disregarding the namespace.<\/strong><\/p>\n<p>XPath is an extensive language, and the examples given here offer only a brief overview. For a more in-depth exploration of this topic, check out our in-depth article: <a href=\"https:\/\/code-maze.com\/csharp-selecting-xml-nodes-with-xpath\/\" target=\"_blank\" rel=\"noopener\">Selecting Xml Nodes With XPath<\/a>.<\/p>\n<h2>Using XmlReader to Read XML Documents in C#<\/h2>\n<p>The second option mentioned for reading XML is the <code>XmlReader<\/code> class.<\/p>\n<p style=\"text-align: left;\"><strong>The <\/strong><code>XmlReader<\/code><strong> class parses the XML one element at a time.<\/strong> However, here we are not talking only about elements such as <code>&lt;name&gt;<\/code>, but also elements such as <code>Attribute<\/code>, <code>Whitespace<\/code>, <code>Text<\/code>, <code>CDATA<\/code>, and others.<\/p>\n<p>All these possibilities are described in an <code>XmlNodeType<\/code> enum. <strong>We use a reader to read one element after another sequentially, and the reader will give us the next element and its type, value, etc<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static IEnumerable&lt;string&gt; ReadXml(string xml)\r\n{\r\n    using var reader = XmlReader.Create(new StringReader(xml));\r\n\r\n    List&lt;string&gt; result = [];\r\n    while (reader.Read())\r\n    {\r\n        result.Add($\"&gt; {reader.NodeType} | {reader.Name} | {reader.Value}\");\r\n    }\r\n\r\n    return result;\r\n}<\/pre>\n<p>Running this method will <strong>produce a long list of different types<\/strong>, but <code>Whitespace<\/code> type may ruin the format as every new line is also a <code>Whitespace<\/code>.<\/p>\n<p>Let&#8217;s remove the whitespaces and see what we get:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static IEnumerable&lt;string&gt; ReadXmlWithoutWhiteSpace(string xml)\r\n{\r\n    using var reader = XmlReader.Create(new StringReader(xml));\r\n\r\n    List&lt;string&gt; result = [];\r\n    while (reader.Read())\r\n    {\r\n        if (reader.NodeType == XmlNodeType.Whitespace)\r\n            continue;\r\n\r\n        result.Add($\"&gt; {reader.NodeType} | {reader.Name} | {reader.Value}\");\r\n    }\r\n\r\n    return result;\r\n}<\/pre>\n<p>This method is very similar,<strong> except it skips all whitespaces<\/strong>. From our sample XML document, we will get output like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">&gt; XmlDeclaration | xml | version=\"1.0\" encoding=\"utf-16\"\r\n&gt; Element | person |\r\n&gt; Element | name |\r\n&gt; Element | firstName |\r\n&gt; Text |  | William\r\n&gt; EndElement | firstName |\r\n&gt; Element | lastName |\r\n&gt; Text |  | Taylor\r\n&gt; EndElement | lastName |\r\n&gt; EndElement | name |\r\n&gt; Element | email |\r\n&gt; Text |  | william.taylor@code-maze.com\r\n&gt; EndElement | email |\r\n&gt; Element | age |\r\n&gt; Text |  | 20\r\n&gt; EndElement | age |\r\n&gt; EndElement | person |<\/pre>\n<p><strong>We can learn a lot by examining the output of the method. <\/strong><\/p>\n<p style=\"text-align: left;\">If nothing more, at least even the basic elements like <code>&lt;lastName&gt;Taylor&lt;\/lastName&gt;<\/code> is split into three parts: <code>Element<\/code>, <code>Text<\/code>, <code>EndElement<\/code>.<\/p>\n<h2>How to Read XML Documents in Practice<\/h2>\n<p>Now that we&#8217;ve understood the intricacies of reading custom XML documents let&#8217;s review a practical example.<\/p>\n<p>We&#8217;ll create a method that enables us <strong>to traverse through a lengthy XML file filled with names and surnames<\/strong>. The objective is to extract both the first and last names efficiently.<\/p>\n<h3>Reading Names and Surnames<\/h3>\n<p>We&#8217;ve already set up an XML file containing personal data with <code>&lt;firstName&gt;<\/code> and <code>&lt;lastName&gt;<\/code> elements. We aim to extract this information and display it in a tabular format.<\/p>\n<p>Firstly, we&#8217;ll define a private class, <code>PersonData<\/code>, with <code>FirstName<\/code> and <code>LastName<\/code> properties to store individual names:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private class PersonData\r\n{\r\n    public string? FirstName { get; set; }\r\n    public string? LastName { get; set; }\r\n    public void Init() =&gt; FirstName = LastName = \"\";\r\n}<\/pre>\n<p>Now, the implementation is straightforward:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static void ReadNamesAndAges(string xml)\r\n{\r\n   var settings = new XmlReaderSettings\r\n   {\r\n       IgnoreWhitespace = true\r\n   };\r\n\r\n   using var reader = XmlReader.Create(new StringReader(xml), settings);\r\n   {\r\n       var personData = new PersonData();\r\n       var numberOfPersons = 0;\r\n\r\n       while (reader.Read())\r\n       {\r\n           if (reader.NodeType == XmlNodeType.Element)\r\n           {\r\n               if (reader.Name == \"person\")\r\n                   personData.Init();\r\n\r\n               if (reader.Name == \"firstName\")\r\n                   personData.FirstName = reader.ReadElementContentAsString();\r\n\r\n               if (reader.Name == \"lastName\")\r\n                   personData.LastName = reader.ReadElementContentAsString();\r\n           }\r\n\r\n           if (reader.NodeType == XmlNodeType.EndElement)\r\n           {\r\n               if (reader.Name == \"person\")\r\n                   Console.WriteLine($\"#{++numberOfPersons,3} | \" +\r\n                       $\"{personData.FirstName,-15} | {personData.LastName,-15}\");\r\n           }\r\n       }\r\n   }\r\n}<\/pre>\n<p>Initially, we set up the <code>XmlReader<\/code> to disregard Whitespaces, simplifying the process. <strong>Here, we employ yet another option to skip whitespace by utilizing the XmlReaderSettings class.<\/strong><\/p>\n<p>As we iterate through elements one by one, when we come across the <code>&lt;person&gt;<\/code> element, we initialize the <code>PersonData<\/code> object. For <code>&lt;firstName&gt;<\/code> or <code>&lt;lastName&gt;<\/code> elements, we update the respective properties.<\/p>\n<p>Upon encountering the <code>&lt;\/person&gt;<\/code> end element, we know that we have all the data for a person, allowing us to print it out. This leads to a well-organized table of people from our XML file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">#  1 | Olivia          | Davis\r\n#  2 | John            | Davis\r\n#  3 | Sarah           | Moore\r\n#  4 | Sarah           | Smith<\/pre>\n<p>The outcome is a well-organized table displaying the sequence number of a person along with their first and last names.<\/p>\n<h2>Conclusion<\/h2>\n<p>Despite JSON becoming the de facto standard for API communication, XML remains integral to many industries due to its longstanding presence. As we inevitably encounter XML standards, we must familiarize ourselves with XML handling in .NET, utilizing classes such as <code>XDocument<\/code>, <code>XmlDocument<\/code>, and <code>XmlReader<\/code>.<\/p>\n<p><code>XDocument<\/code> is a versatile tool for most tasks, simplifying the reading of diverse XML documents. <span style=\"font-weight: 400;\">For complex or poorly structured XML documents, the advanced capabilities of <code>XmlReader<\/code> are essential.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Regardless of your content or its complexity, mastering these tools is key to effective XML handling in .NET.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll talk about how to read XML Documents in C#. In the preceding article, we addressed the creation of custom XML documents. Also, we have already explored how to serialize and deserialize objects to and from XML in the articles titled Serializing Objects to XML in C# and\u00a0XML Deserialization in C#. So [&hellip;]<\/p>\n","protected":false},"author":53,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1233,1033],"tags":[946,1201,2074,2055,2059],"class_list":["post-104103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linq","category-xml","tag-collections","tag-linq-to-xml","tag-read-xml-files","tag-xdocument","tag-xml-linqtoxml","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Read XML Documents in C#<\/title>\n<meta name=\"description\" content=\"Let&#039;s learn how to read XML documents in C# using XDocument and XmlWriter, including attributes, namespaces, and converting from CSV.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Read XML Documents in C#\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s learn how to read XML documents in C# using XDocument and XmlWriter, including attributes, namespaces, and converting from CSV.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-29T12:47:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-31T14:51:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Matjaz Prtenjak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Matjaz Prtenjak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\"},\"author\":{\"name\":\"Matjaz Prtenjak\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/fb52db5fd702889fb141605f63bff589\"},\"headline\":\"How to Read XML Documents in C#\",\"datePublished\":\"2024-01-29T12:47:32+00:00\",\"dateModified\":\"2024-01-31T14:51:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\"},\"wordCount\":1134,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"collections\",\"LINQ to XML\",\"Read XML files\",\"XDocument\",\"XML. LinqToXml\"],\"articleSection\":[\"LINQ\",\"XML\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\",\"url\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\",\"name\":\"How to Read XML Documents in C#\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2024-01-29T12:47:32+00:00\",\"dateModified\":\"2024-01-31T14:51:18+00:00\",\"description\":\"Let's learn how to read XML documents in C# using XDocument and XmlWriter, including attributes, namespaces, and converting from CSV.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Read XML Documents in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/fb52db5fd702889fb141605f63bff589\",\"name\":\"Matjaz Prtenjak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/MatjazPrtenjak_400x400-150x150.jpg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/MatjazPrtenjak_400x400-150x150.jpg\",\"caption\":\"Matjaz Prtenjak\"},\"description\":\"Matjaz is a seasoned professional with over 25 years of expertise in software development. He began his programming journey during the era when IBM dominated with mainframe computers, and personal computers were yet to be born. He possesses a rich background and has authored two books on C++ and VBA programming languages. Lately, he has dedicated himself to harnessing the power of .NET technology in his innovative projects.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/mprtenja\/\"],\"url\":\"https:\/\/code-maze.com\/author\/mprtenjak\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Read XML Documents in C#","description":"Let's learn how to read XML documents in C# using XDocument and XmlWriter, including attributes, namespaces, and converting from CSV.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/","og_locale":"en_US","og_type":"article","og_title":"How to Read XML Documents in C#","og_description":"Let's learn how to read XML documents in C# using XDocument and XmlWriter, including attributes, namespaces, and converting from CSV.","og_url":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/","og_site_name":"Code Maze","article_published_time":"2024-01-29T12:47:32+00:00","article_modified_time":"2024-01-31T14:51:18+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Matjaz Prtenjak","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Matjaz Prtenjak","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/"},"author":{"name":"Matjaz Prtenjak","@id":"https:\/\/code-maze.com\/#\/schema\/person\/fb52db5fd702889fb141605f63bff589"},"headline":"How to Read XML Documents in C#","datePublished":"2024-01-29T12:47:32+00:00","dateModified":"2024-01-31T14:51:18+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/"},"wordCount":1134,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["collections","LINQ to XML","Read XML files","XDocument","XML. LinqToXml"],"articleSection":["LINQ","XML"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-reading-xml-documents\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/","url":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/","name":"How to Read XML Documents in C#","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2024-01-29T12:47:32+00:00","dateModified":"2024-01-31T14:51:18+00:00","description":"Let's learn how to read XML documents in C# using XDocument and XmlWriter, including attributes, namespaces, and converting from CSV.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-reading-xml-documents\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-reading-xml-documents\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Read XML Documents in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/fb52db5fd702889fb141605f63bff589","name":"Matjaz Prtenjak","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/MatjazPrtenjak_400x400-150x150.jpg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/MatjazPrtenjak_400x400-150x150.jpg","caption":"Matjaz Prtenjak"},"description":"Matjaz is a seasoned professional with over 25 years of expertise in software development. He began his programming journey during the era when IBM dominated with mainframe computers, and personal computers were yet to be born. He possesses a rich background and has authored two books on C++ and VBA programming languages. Lately, he has dedicated himself to harnessing the power of .NET technology in his innovative projects.","sameAs":["https:\/\/www.linkedin.com\/in\/mprtenja\/"],"url":"https:\/\/code-maze.com\/author\/mprtenjak\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/104103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/53"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=104103"}],"version-history":[{"count":9,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/104103\/revisions"}],"predecessor-version":[{"id":104115,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/104103\/revisions\/104115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=104103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=104103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=104103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}