Skip to content

Instantly share code, notes, and snippets.

@tmori3y2
Last active June 22, 2017 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmori3y2/0745bcc605ec304eede0106cfd6f5f40 to your computer and use it in GitHub Desktop.
Save tmori3y2/0745bcc605ec304eede0106cfd6f5f40 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<Output>DataGrids</Output>
<Reference>&lt;RuntimeDirectory&gt;\System.Xml.Serialization.dll</Reference>
<Namespace>System.Globalization</Namespace>
<Namespace>System.IO</Namespace>
<Namespace>System.Linq</Namespace>
<Namespace>System.Text</Namespace>
<Namespace>System.Xml</Namespace>
<Namespace>System.Xml.Linq</Namespace>
<Namespace>System.Xml.Schema</Namespace>
<Namespace>System.Xml.Serialization</Namespace>
</Query>
// LinqPad 5で実行可能な C# script.
// 開発環境が無くても実行できます.
// 以下からダウンロードできます:
// https://www.linqpad.net/
// OS: Windows 7 sp1 or higher
// .NET Framework: 4.6 or higher
// Windows 10には含まれています.
// Windows 7 sp1/8.1は以下からダウンロードしてください:
// https://www.microsoft.com/ja-jp/download/details.aspx?id=53344
void Main()
{
var defaultTest = new TestGuid();
defaultTest.DumpObject();
var test = new TestGuid()
{
GuidA = Guid.NewGuid(),
Guid = Guid.NewGuid(),
NullableGuidA = Guid.NewGuid(),
NullableGuid = Guid.NewGuid(),
GuidString = Guid.NewGuid().ToString(),
GuidStringA = Guid.NewGuid().ToString(),
AttributableGuidA = Guid.NewGuid(),
AttributableGuid = Guid.NewGuid(),
HandledGuidA = Guid.NewGuid(),
HandledGuid = Guid.NewGuid(),
GuidWrapperA = (XmlGuid)Guid.NewGuid(),
GuidWrapper = Guid.NewGuid(),
CastedGuid = Guid.NewGuid()
};
test.DumpObject();
}
// Define other methods and classes here
public static class GuidHelper
{
public static bool IsEmpty(this Guid value) => value == Guid.Empty;
public static bool IsValidGuid (this string value)
{
Guid guid;
return Guid.TryParseExact(value, "D", out guid);
}
public static Guid ToGuid(this string value) => Guid.ParseExact(value, "D");
}
[XmlType("testType")]
[XmlRoot("test")]
public class TestGuid
{
[XmlAttribute("guid")]
public Guid GuidA { get; set; }
[XmlElement("Guid")]
public Guid Guid { get; set; }
[XmlIgnore]
// Cannot serialize System.Nullable. XmlAttribute/XmlText cannot be used to encode complex types.
//[XmlAttribute("nullableGuid")]
public Guid? NullableGuidA { get; set; }
[XmlElement("NullableGuid")]
public Guid? NullableGuid { get; set; }
[XmlAttribute("guidString")]
public string GuidStringA
{
get { return _guidStringA?.ToString(); }
set { _guidStringA = value?.ToGuid(); }
}
private Guid? _guidStringA;
[XmlElement("GuidString")]
public string GuidString
{
get { return _guidString?.ToString(); }
set { _guidString = value?.ToGuid(); }
}
private Guid? _guidString;
// https://msdn.microsoft.com/ja-jp/library/system.xml.serialization.xmlserializer(v=vs.110).aspx#Anchor_5
// https://msdn.microsoft.com/ja-jp/library/system.xml.serialization.xmlignoreattribute(v=vs.110).aspx#Anchor_6
[XmlAttribute("attributableGuid")]
public Guid AttributableGuidA { get; set; }
[XmlIgnore]
public bool AttributableGuidASpecified => !AttributableGuidA.IsEmpty();
[XmlElement("AttributableGuid")]
public Guid AttributableGuid { get; set; }
[XmlIgnore]
public bool AttributableGuidSpecified => !AttributableGuid.IsEmpty();
// https://msdn.microsoft.com/en-us/library/53b8022e.aspx
[XmlAttribute("handledGuid")]
public Guid HandledGuidA { get; set; }
public bool ShouldSerializeHandledGuidA() => !HandledGuidA.IsEmpty();
[XmlElement("HandledGuid")]
public Guid HandledGuid { get; set; }
public bool ShouldSerializeHandledGuid() => !HandledGuid.IsEmpty();
[XmlAttribute("guidWrapper")]
public string GuidWrapperA
{
get { return _guidWrapperA; }
set { _guidWrapperA = value; }
}
private XmlGuid _guidWrapperA;
[XmlElement("GuidWrapper")]
public XmlGuid GuidWrapper { get; set; }
// https://stackoverflow.com/questions/637933/how-to-serialize-a-timespan-to-xml
[XmlElement("CastedGuid", typeof(XmlGuid))]
public Guid? CastedGuid { get; set; }
public void DumpObject()
{
this.Dump();
var xml = this.XmlSerialize("");
xml.Dump();
var deserialized = xml.XmlDeserialize<TestGuid>("");
deserialized.Dump();
}
}
public class XmlGuid
{
public XmlGuid()
{
}
public XmlGuid(Guid source)
{
this.Value = source;
}
public XmlGuid(string source)
{
this.SetString(source);
}
[XmlText]
public string Text
{
get { return this.ToString(); }
set { this.SetString(value); }
}
[XmlIgnore]
public Guid Value { get; set; }
public static implicit operator Guid?(XmlGuid source) => source?.Value;
public static implicit operator XmlGuid(Guid? source) => source ?? Guid.Empty;
public static implicit operator Guid(XmlGuid source) => source?.Value ?? Guid.Empty;
public static implicit operator XmlGuid(Guid source) => source != Guid.Empty ? new XmlGuid(source) : null;
public static implicit operator string(XmlGuid source) => source?.ToString();
public static implicit operator XmlGuid(string source)
{
Guid guid;
return Guid.TryParseExact(source, "D", out guid) ? new XmlGuid(guid) : null;
}
public override string ToString() => this.Value.ToString();
public void SetString(string value)
{
this.Value = (XmlGuid)value ?? Guid.Empty;
}
}
public static class XmlSerializeHelper
{
public static string XmlSerialize<T>(this T source, string defaultNamespace)
{
if (source == null)
{
return null;
}
var encoding = new UTF8Encoding(false);
var settings = new XmlWriterSettings();
settings.Encoding = encoding;
settings.Indent = true;
byte[] bytes = null;
var serializer = new XmlSerializer(typeof(T), defaultNamespace);
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, source);
}
bytes = stream.ToArray();
}
return (bytes != null) ? encoding.GetString(bytes, 0, bytes.Length) : null;
}
public static T XmlDeserialize<T>(this string xml, string defaultNamespace)
{
if (string.IsNullOrEmpty(xml))
{
return default(T);
}
var encoding = new UTF8Encoding(false);
var settings = new XmlReaderSettings();
var byteArray = encoding.GetBytes(xml);
T target = default(T);
var serializer = new XmlSerializer(typeof(T), defaultNamespace);
using (var stream = new MemoryStream(byteArray))
{
using (var reader = XmlReader.Create(stream, settings))
{
target = (T)serializer.Deserialize(reader);
}
}
return target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment