xmlserializer_使用XmlSerializer读取和写入XML片段

xmlserializer

xmlserializer

This may not be interesting to you if you don't like the XmlSerializer. I use it for lots of stuff and I'm currently using it in the middle of an XmlReader/XmlWriter pipeline to grab off little object chunks via reader.ReadOuterXml (a .NET 1.1's poorman's ReadSubTree). I've got schemas for my objects that they are generated from, and as schema, they have namespaces. However, the Xml Fragments I'm grabbing off do not have namespaces, and sometimes the document doesn't (don't ask, sometimes life doesn't turn out how you'd like, eh?).

如果您不喜欢XmlSerializer,这可能对您没有兴趣。 我用它来做很多事情,目前我在XmlReader / XmlWriter管道的中间使用它,以通过reader.ReadOuterXml(.NET 1.1的可怜人ReadSubTree )获取小对象块。 我有从中生成对象的模式,作为模式,它们具有名称空间。 但是,我要获取的Xml Fragments没有命名空间,有时文档没有命名空间(不要问,有时候生活并不能证明您想要的方式,是吗?)。

So, I need to be able to read and write Xml Fragments into and out of objects via the XmlSerializer. This uses a view techniques I've covered before like the XmlFragmentWriter (Yes there are other ways) and the XmlNamespaceUpgradeReader. I added a few properties like SuppressAllNamespaces and JustRoot to really make these bare XmlFragments.

因此,我需要能够通过XmlSerializer读写对象中的Xml片段。 这使用了我以前介绍过的视图技术,例如XmlFragmentWriter (是的,还有其他方法)和XmlNamespaceUpgradeReader 。 我添加了一些属性,例如SuppressAllNamespaces和JustRoot,以真正制作这些裸露的XmlFragments。

[Test]

[测试]

public void TestRoundTrip()

公共无效TestRoundTrip()

{

{

    AccountType acct = new AccountType();

AccountType acct = new AccountType();

    acct.AvailableBalance = 34.33M;

acct.AvailableBalance = 34.33M;

    acct.AvailableBalanceSpecified = true;

acct.AvailableBalanceSpecified = true ;

    acct.Number = "54321";

acct.Number =“ 54321”;

    acct.Description = "My Checking";

acct.Description =“我的支票”;

    XmlSerializer ser = new XmlSerializer(typeof(AccountType));

XmlSerializer ser =新的XmlSerializer( typeof (AccountType));

    //***WRITE***

//***写***

    StringBuilder sb = new StringBuilder();

StringBuilder sb = new StringBuilder();

    using(StringWriter sw = new StringWriter(sb))

使用(StringWriter sw = new StringWriter(sb))

    {

{

        XmlFragmentWriter fragWriter = new XmlFragmentWriter(sw);

XmlFragmentWriter fragWriter =新的XmlFragmentWriter(sw);

        fragWriter.SuppressAllNamespaces = true;

fragWriter.SuppressAllNamespaces = true ;

        ser.Serialize(fragWriter,acct);

ser.Serialize(fragWriter,acct);

        fragWriter.Close();

fragWriter.Close();

    }

}

    string result = sb.ToString();

字符串结果= sb.ToString();

    //***READ***

//***读***

    AccountType acctReborn = null;

AccountType acctReborn = null ;

    using(StringReader sr = new StringReader(result))

使用(StringReader sr = new StringReader(result))

    {

{

        acctReborn = ser.Deserialize(

acctReborn = ser.Deserialize(

            new XmlNamespaceUpgradeReader(sr,

新的XmlNamespaceUpgradeReader(sr,

            String.Empty,

String.Empty,

            "http://banking.corillian.com/Account.xsd")) as AccountType;

“ http://banking.corillian.com/Account.xsd”))作为AccountType;

    }

}

    Assert.IsTrue(acctReborn.AvailableBalance == 34.33M);

Assert.IsTrue(acctReborn.AvailableBalance == 34.33M);

}

}

Enjoy, improve, give back. File Attachment: SerializationFragmentTest.zip (5 KB)

享受,改善,回馈。 文件附件:SerializationFragmentTest.zip(5 KB)

翻译自: https://www.hanselman.com/blog/using-the-xmlserializer-to-read-and-write-xml-fragments

xmlserializer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值