Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using XMLSerializationInCsharp_v1;

namespace XMLSerializationInCsharp
var patient = new Patient()
{
ID = 232323,
FirstName = "John",
LastName = "Doe",
Birthday = new DateTime(1990, 12, 30),
RoomNo = 310
};

class Program
{
static void Main(string[] args)
{
Console.Write("hello world");
}
}
// The article's first example: a StreamWriter over a file.
var serializer = new XmlSerializer(typeof(Patient));
using (var writer = new StreamWriter("patients.xml"))
{
serializer.Serialize(writer, patient);
}

Console.WriteLine(File.ReadAllText("patients.xml"));
Console.WriteLine();

// The article's last example: an XmlWriter with indentation, UTF-8 without a byte order mark,
// and no xmlns:xsi or xmlns:xsd declarations on the root element.
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

var settings = new XmlWriterSettings
{
Indent = true,
Encoding = new UTF8Encoding(false)
};

using (var xmlWriter = XmlWriter.Create("patient.xml", settings))
{
serializer.Serialize(xmlWriter, patient, namespaces);
}

Console.WriteLine(File.ReadAllText("patient.xml"));
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="NUnit" Version="3.13.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="NUnit" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using NUnit.Framework;
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using XMLSerializationInCsharp_v1;

namespace XMLSerializationInCsharpTests
{
public class XMLSerializationUnitTest_v6
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Patient>
<ID>232323</ID>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Birthday>1990-12-30T00:00:00</Birthday>
<RoomNo>310</RoomNo>
</Patient>";

[Test]
public void WhenSerializingWithXmlWriterSettingsAndEmptyNamespaces_ThenNoNamespacesAndUtf8Declaration()
{
var patient = new Patient()
{
ID = 232323,
FirstName = "John",
LastName = "Doe",
Birthday = new DateTime(1990, 12, 30),
RoomNo = 310
};

var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

var settings = new XmlWriterSettings
{
Indent = true,
Encoding = new UTF8Encoding(false)
};

var serializer = new XmlSerializer(typeof(Patient));
using var stream = new MemoryStream();
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, patient, namespaces);
}

var bytes = stream.ToArray();
var result = Encoding.UTF8.GetString(bytes);

Assert.That(bytes[0], Is.EqualTo((byte)'<'), "UTF8Encoding(false) writes no byte order mark");
Assert.That(result, Does.Not.Contain("xmlns:xsi"));
Assert.That(result, Does.Not.Contain("xmlns:xsd"));
Assert.That(result.Replace("\r\n", "\n"), Is.EqualTo(xml.Replace("\r\n", "\n")));
}
}
}
Loading