Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.
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
18 changes: 18 additions & 0 deletions BookUpdates.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ public static MySafe InitProtection()
}
```

## Chapter 27 - XML and JSON

Page 812, the Load method of the XDocument class supports only loading of local files (the .NET Framework version of this method supports loading files from HTTP servers as well). The sample code for the `QueryFeed` method needs to be changed. The HttpClient class is used to make a HTTP GET requests to return a stream. The stream is passed to the XDocument.Load method:

```csharp
public static async void QueryFeed()
{
try
{
var httpClient = new HttpClient();
using (Stream stream = await httpClient.GetStreamAsync("http://csharp.christiannagel.com/feed/atom/"))
{
XNamespace ns = "http://www.w3.org/2005/Atom";
XDocument doc = XDocument.Load(stream);

WriteLine($"Title: {doc.Root.Element(ns + "title").Value}");
```

## Chapter 29 - Core XAML

Page 852, the minimum UWP application needs a change to use a custom class derived from the *Application* class. The *Main* method is now changed to this:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"JsonSample": {
"commandName": "JsonSample",
"commandName": "Project",
"commandLineArgs": "-c"
}
}
Expand Down
64 changes: 40 additions & 24 deletions XMLandJSON/XMLandJSONSamples/LinqToXmlSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Xml.Linq;
using static System.Console;

Expand Down Expand Up @@ -66,6 +68,9 @@ static void Main(string[] args)
ShowUsage();
break;
}

WriteLine("Press return to exit");
ReadLine();
}

private static void ShowUsage()
Expand Down Expand Up @@ -192,34 +197,45 @@ public static void QueryHamlet()
}
}

public static void QueryFeed()
public static async void QueryFeed()
{
XNamespace ns = "http://www.w3.org/2005/Atom";
XDocument doc = XDocument.Load(@"http://blog.cninnovation.com/feed/atom/");

WriteLine($"Title: {doc.Root.Element(ns + "title").Value}");
WriteLine($"Subtitle: {doc.Root.Element(ns + "subtitle").Value}");
string url = doc.Root.Elements(ns + "link").Where(e => e.Attribute("rel").Value == "alternate").FirstOrDefault()?.Attribute("href")?.Value;
WriteLine($"Link: {url}");
WriteLine();
try
{
var httpClient = new HttpClient();
using (Stream stream = await httpClient.GetStreamAsync("http://csharp.christiannagel.com/feed/atom/"))
{

var queryPosts = from myPosts in doc.Descendants(ns + "entry")
select new
{
Title = myPosts.Element(ns + "title")?.Value,
Published =
DateTime.Parse(myPosts.Element(ns + "published")?.Value),
Summary = myPosts.Element(ns + "summary")?.Value,
Url = myPosts.Element(ns + "link")?.Value,
Comments = myPosts.Element(ns + "comments")?.Value
};

foreach (var item in queryPosts)
XNamespace ns = "http://www.w3.org/2005/Atom";
XDocument doc = XDocument.Load(stream);

WriteLine($"Title: {doc.Root.Element(ns + "title").Value}");
WriteLine($"Subtitle: {doc.Root.Element(ns + "subtitle").Value}");
string url = doc.Root.Elements(ns + "link").Where(e => e.Attribute("rel").Value == "alternate").FirstOrDefault()?.Attribute("href")?.Value;
WriteLine($"Link: {url}");
WriteLine();

var queryPosts = from myPosts in doc.Descendants(ns + "entry")
select new
{
Title = myPosts.Element(ns + "title")?.Value,
Published =
DateTime.Parse(myPosts.Element(ns + "published")?.Value),
Summary = myPosts.Element(ns + "summary")?.Value,
Url = myPosts.Element(ns + "link")?.Value,
Comments = myPosts.Element(ns + "comments")?.Value
};

foreach (var item in queryPosts)
{
string shortTitle = item.Title.Length > 50 ? item.Title.Substring(0, 50) + "..." : item.Title;
WriteLine(shortTitle);
}
}
}
catch (Exception ex)
{
string shortTitle = item.Title.Length > 50 ? item.Title.Substring(0, 50) + "..." : item.Title;
WriteLine(shortTitle);
WriteLine(ex.Message);
}

}

public static void TransformingToObjects()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"profiles": {
"LinqToXmlSample": {
"commandName": "LinqToXmlSample",
"commandLineArgs": "-t"
"commandName": "Project",
"commandLineArgs": "-f"
}
}
}
2 changes: 2 additions & 0 deletions XMLandJSON/XMLandJSONSamples/LinqToXmlSample/project.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},

"dependencies": {
"System.Net.Http": "4.3.0"
},

"frameworks": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"ObjectToXmlSerializationSample": {
"commandName": "ObjectToXmlSerializationSample",
"commandName": "Project",
"commandLineArgs": "-st"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"ObjectToXmlSerializationWOAttributes": {
"commandName": "ObjectToXmlSerializationWOAttributes",
"commandName": "Project",
"commandLineArgs": "-d"
}
}
Expand Down
7 changes: 6 additions & 1 deletion XMLandJSON/XMLandJSONSamples/XMLandJSONSamples.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "XmlDocumentSample", "XmlDocumentSample\XmlDocumentSample.xproj", "{9E4D60F9-286C-4971-BECB-C75315AE60CB}"
EndProject
Expand All @@ -17,6 +17,11 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "XmlReaderAndWriterSample",
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "JsonSample", "JsonSample\JsonSample.xproj", "{40BB40F6-EA0F-42FC-8BD1-A94B43932A66}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7E01E6BF-6098-4BB2-84A6-DB61ED4C7A3F}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"XPathNavigatorSample": {
"commandName": "XPathNavigatorSample",
"commandName": "Project",
"commandLineArgs": "-e"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"XmlDocumentSample": {
"commandName": "XmlDocumentSample",
"commandName": "Project",
"commandLineArgs": "-w"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"XmlReaderAndWriterSample": {
"commandName": "XmlReaderAndWriterSample",
"commandName": "Project",
"commandLineArgs": "-c"
}
}
Expand Down
8 changes: 8 additions & 0 deletions XMLandJSON/XMLandJSONSamples/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"projects": [
"."
],
"sdk": {
"version": "1.0.0-preview2-003131"
}
}