一瞬间回到了 Visual Basic 6.0 时代……
一把辛酸一把泪。

以及

这样根本没法调试好不……
最后只得使用 Debug.Print ,还用的是 Expression……

System.WeakReference<T>
一瞬间回到了 Visual Basic 6.0 时代……
一把辛酸一把泪。

以及

这样根本没法调试好不……
最后只得使用 Debug.Print ,还用的是 Expression……
cite=LINQ to SQL FAQ – XML columns in SQL
However, LINQ to SQL does not appear to support ANY translations of XML queries on XElement to SQL – this mans that the following statement :
123 var filtered = from t in Tablewhere t.XmlCol.Descendants("SomeElement").Value == somevarselect twill fail at runtime as the LINQ-to-SQL parser is unable to translate on our XML data column XmlCol query into an Xpath.
A LINQ to SQL xml datatype column is represented as an XElement. If you modify this be careful as LINQ to SQL will not spot internal changes to XElement and save them to the database.
For example:
123 Dim myElement as XElement = Table.XMLdatamyElement.Value = "test"db.SubmitChanges()will not alter the instance XElement and so will not change the Table.XMLdata entry. The workaround for this is to write it back as a new element:
1234 Dim myElement as XElement = Table.XMLdatamyElement.Value = "test"Table.XMLdata = new XElement(myElement)db.SubmitChanges()
今天试了一下,发现WebClient虽然支持异步,但果然不支持并发……
继续阅读“WebClient 不支持并发操作”
请原谅我之前的愚昧无知。我已经决定完全分离对象模型和对象的逻辑行为了。
引用使用 Id 保存,即类中不再出现对其他类的直接引用,而仅仅保存 Id。这样在序列化的时候就可以直接保存了。

然而我发现处理 Nullable 类型还是一个很麻烦的问题……
c# – Is there any way for my class to support serialisation as an XML attribute? – Stack Overflow
所以我要自己造轮子!
重新造轮子 TT
ref = CXuesong/XSerializer/
欢迎围观 TT
最近一直在考虑以下问题:
在使用 XmlSerializer 以精确控制 XML 格式的同时
Nullable<T> 格式的数据,并将其保存为 XML 属性(而非元素)?Nullable<T> 与 string 之间的转换,而 string 是可以保存为 XML 属性的。)其中,第二个问题似乎可以使用 DataContractSeriallizer 来解决。然而 DataContractSeriallizer 会在生成 XML 时自行为对象确定 Id,并使用 z:Id 和z:Ref 来表示 Id 和 Id 引用。正如在前几篇文章中提到的那样,DataContractSeriallizer 对 XML 结构的控制自由度是十分有限的。
那么,如何才能圆满地解决这些问题呢?
我觉得,可以自己造轮子了。
通常来说,当你想更加精确地控制生成的 XML 时,可以使用 XmlSerializer。如果你对生成的 XML 格式并不在意(例如所有的属性/字段均会以元素的形式保存),同时想保留对象的所有信息(包括私有属性/字段),则可以使用 DataContractSerializer。 继续阅读“在 XmlSerializer 与 DataContractSerializer 之间抉择”
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Collections.Generic; using System.Xml.Serialization; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var f = new XmlSerializer(typeof(TestClass)); using (var sw = new StringWriter()) { f.Serialize(sw, new TestClass()); using (var sr = new StringReader(sw.ToString())) { var bt = (TestClass) f.Deserialize(sr); Console.WriteLine(bt.ListIdentical()); } } } } public class TestClass { private List<int> PrivateList; public List<int> MyList; public bool ListIdentical() { return PrivateList == MyList; } public TestClass() { PrivateList = new List<int>() { 123, 567 }; MyList = PrivateList; } } } |
运行结果为 True 。实际上,如果把 MyList 换成只读属性,只要其值非 null / Nothing,则程序仍可以正常运行。
也就是说,反序列化过程复用了已有的集合实例。 继续阅读“XmlSerializer 测试 2”
好吧…… fonts.lug.ustc.edu.cn 貌似挂掉了。现在已经打算在本站嵌入字体了。
与此同时,发现 Useso take over Google 的作者已经开始着手修改源码了。以下是 1.6.1 版本的代码片段:
|
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
function useso_take_over_google_str_handler($matches) { $str = $matches[0]; if (!is_ssl()) { $str = str_ireplace('//fonts.googleapis.com/', '//fonts.useso.com/', $str); $str = str_ireplace('//ajax.googleapis.com/', '//ajax.useso.com/', $str); //$str = str_ireplace('//fonts.googleapis.com/', '//fonts.geekzu.org/', $str); //$str = str_ireplace('//ajax.googleapis.com/', '//fdn.geekzu.org/ajax/', $str); } else { //$str = str_ireplace('//fonts.googleapis.com/', '//fonts.lug.ustc.edu.cn/', $str); //$str = str_ireplace('//ajax.googleapis.com/', '//ajax.lug.ustc.edu.cn/', $str); $str = str_ireplace('//fonts.googleapis.com/', '//fonts.geekzu.org/', $str); $str = str_ireplace('//ajax.googleapis.com/', '//sdn.geekzu.org/ajax/', $str); } return $str; } |
然后就发现了 极客族公共加速服务 ……
p.s.
我觉得 Useso take over Google 可以改名叫 “Geeks” take over Google 了 XD
测试源代码如下,使用 VS2013 编写。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using System.Collections; using System.Collections.Generic; using System.Xml.Serialization; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var f = new XmlSerializer(typeof(TestClass), new[] { typeof(DerivedClass1), typeof(ChildNS.DerivedClass1) }); var ns = new XmlSerializerNamespaces(); ns.Add("ns", "http://yourcompany.org/schemas/ns"); ns.Add("nsc", "http://yourcompany.org/schemas/ns/child"); ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); using (var sw = new StringWriter()) { f.Serialize(sw, new TestClass(), ns); Console.WriteLine(sw.ToString()); } } } public class TestClass { public BaseClass BaseTest = new BaseClass(); public BaseClass BaseTest1 = new DerivedClass1(); [XmlElement(Namespace = "http://yourcompany.org/schemas/ns/child")] public BaseClass BaseTest2 = new ChildNS.DerivedClass1(); public BaseClass[] BaseArray1 = {new BaseClass(), new DerivedClass1()}; [XmlArrayItem(typeof(BaseClass), IsNullable = true), XmlArrayItem(typeof(DerivedClass1), IsNullable = true), XmlArrayItem(typeof(DerivedClass2), IsNullable = true)] public BaseClass[] BaseArray2 = { new BaseClass(), new DerivedClass1(), null, new DerivedClass2() }; [XmlArrayItem(typeof(BaseClass), IsNullable = true)] public BaseClass[] BaseArray3 = { new BaseClass(), new DerivedClass1(), null, new DerivedClass2() }; [XmlArrayItem(typeof(BaseClass))] public List<BaseClass> BaseList = new List<BaseClass> { new BaseClass(), new DerivedClass1() }; [XmlElement(IsNullable = true)] public object NullObject1 = null; public object NullObject2 = null; //public IEnumerable BaseListEnumerable = new List<BaseClass> { new BaseClass(), new DerivedClass1() }; } public class BaseClass { [XmlAttribute("value1", Namespace = "http://yourcompany.org/schemas/ns")] public int Value1; [XmlAttribute("value2")] public int Value2; [XmlAttribute("value3", Namespace = "http://yourcompany.org/schemas/ns")] public int Value3; } public class DerivedClass1 : BaseClass { [XmlAttribute("value4", Namespace = "http://yourcompany.org/schemas/ns")] public int Value4; } public class DerivedClass2 : BaseClass { [XmlAttribute("value4", Namespace = "http://yourcompany.org/schemas/ns")] public int Value4; } namespace ChildNS { //为类型强制指定 XML 名称,以及命名空间,以避免命名冲突。 [XmlType("ChildDerivedClass", Namespace = "http://yourcompany.org/schemas/ns/child")] public class DerivedClass1 : BaseClass { public int Value5; } } } |
运行结果如下
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="utf-16"?> <TestClass xmlns:ns="http://yourcompany.org/schemas/ns" xmlns:nsc="http://yourcompany.org/schemas/ns/child" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <BaseTest ns:value1="0" value2="0" ns:value3="0" /> <BaseTest1 xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> <nsc:BaseTest2 xsi:type="nsc:ChildDerivedClass" ns:value1="0" value2="0" ns:value3="0"> <nsc:Value5>0</nsc:Value5> </nsc:BaseTest2> <BaseArray1> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <BaseClass xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseArray1> <BaseArray2> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <DerivedClass1 ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> <DerivedClass2 ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseArray2> <BaseArray3> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <BaseClass xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> <BaseClass xsi:nil="true" /> <BaseClass xsi:type="DerivedClass2" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseArray3> <BaseList> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <BaseClass xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseList> <NullObject1 xsi:nil="true" /> </TestClass> |
可以看出,
XmlArrayItem(Attribute)、XmlElement 等相关特性中指定
IsNullable = true 可以使得当前为 null / Nothing 的元素在序列化时产生一个包含
xsi:nil="true" 特性的 XML 元素。XmlSerializerNamespaces 中为这些 URI 指定对应的命名空间前缀。XmlSerializer 的构造函数显式声明序列化过程中可能用到的派生类。XmlArrayItem 特性声明数组中可能包含的派生类类型。这些类型在序列化时会使用与之对应的元素名。如果存在未在 XmlArrayItem 中声明的派生类,而其基类在 XmlArrayItem 中声明过,则会使用基类对应的元素名,外加 xsi:type 特性声明实际派生类的类型。(关于这一点,可以参阅另一篇文章:MSDN中“(使用 XmlArrayItemAttribute 限定)序列化派生类”一节中的示例可能与实际有出入)