XmlFragmentWriter-省略Xml声明以及XSD和XSI命名空间

There' s a pretty nasty XmlFragmentWriter example up on GDN that uses reflection to mess with the internal state of an XmlTextWriter in order to omit the XML declaration. Yikes.

在GDN上有一个非常讨厌的XmlFragmentWriter示例,示例使用反射来弄乱XmlTextWriter的内部状态,以便省略XML声明。 kes。

This is an alternate (better) XmlFragmentWriter that's breaks fewer Commandments. It takes code from Sairama, one of our platform engineers at Corillian, to omit the XmlDecl. I took Sai's stuff and added Kzu's xsi/xsd trick to create XML fragments. Here's XmlFragmentWriter.

这是XmlFragmentWriter(更好的),它打破了更少的Commandments。 它需要来自Corillian的一位平台工程师Sairama的代码来省略XmlDecl。 我采用了Sai的资料,并添加了Kzu的xsi / xsd技巧来创建XML片段。 这是XmlFragmentWriter。

Given a class (just an example, don't serialize passwords!):

给定一个类(仅作为示例,请勿序列化密码!):

public class AuthenticationInfo

公共类AuthenticationInfo

{

{

    public string Username;

公共字符串用户名;

    public string Password;

公共字符串密码;

}

}

Here's the code and an instance serialized using the standard XmlSerializer. Note the Xml Declaration and the XML schema and instance namespace:

这是使用标准XmlSerializer序列化的代码和实例。 请注意Xml声明以及XML模式和实例名称空间:

<?xml version="1.0"?>
<AuthenticationInfo
      xmlns:xsd="
http://www.w3.org/2001/XMLSchema
      xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">
  <Username>user1</Username>
  <Password>pass1</Password>
</AuthenticationInfo>

<?xml version =“ 1.0”?> <AuthenticationInfo xmlns:xsd =“ http://www.w3.org/2001/XMLSchema xmlns:xsi =“ http://www.w3.org/2001/XMLSchema-instance ”> <用户名>用户1 </用户名> <Password> pass1 </ Password> </ AuthenticationInfo>

Standard XmlSerializer fare: 

标准XmlSerializer票价:

AuthenticationInfo a = new AuthenticationInfo();

AuthenticationInfo a = new AuthenticationInfo();

a.Username = "user1";

a.Username =“ user1”;

a.Password = "pass1";

a.Password =“ pass1”;

XmlSerializer x = new XmlSerializer( typeof(AuthenticationInfo));

XmlSerializer x =新的XmlSerializer( typeof (AuthenticationInfo));

XmlTextWriter w2 = new XmlTextWriter(@"c:\bar.xml",null);

XmlTextWriter w2 =新的XmlTextWriter(@“ c:\ bar.xml”, null );

w2.Formatting = Formatting.Indented;

w2.Formatting = Formatting.Indented;

x.Serialize( w2, a );

x.Serialize(w2,a);

w2.Close();

w2.Close();

Here's the same object serialized using our XmlFragmentWriter: 

这是使用XmlFragmentWriter序列化的同一对象:

<AuthenticationInfo>
  <Username>user1</Username>
  <Password>pass1</Password>
</AuthenticationInfo>

<AuthenticationInfo> <用户名>用户1 </用户名> <Password> pass1 </ Password> </ AuthenticationInfo>

And here's how it's used:

以及它的用法:

AuthenticationInfo a = new AuthenticationInfo();

AuthenticationInfo a = new AuthenticationInfo();

a.Username = "user1";

a.Username =“ user1”;

a.Password = "pass1";

a.Password =“ pass1”;

XmlSerializer f = new XmlSerializer( typeof(AuthenticationInfo));

XmlSerializer f =新的XmlSerializer( typeof (AuthenticationInfo));

XmlFragmentWriter w = new XmlFragmentWriter(@"c:\foo.xml",null);

XmlFragmentWriter w =新的XmlFragmentWriter(@“ c:\ foo.xml”, null );

w.Formatting = Formatting.Indented;

w.Formatting = Formatting.Indented;

f.Serialize( w, a );

f.Serialize(w,a);

w.Close();

w.Close();

And here's the XmlFragmentWriter class:

这是XmlFragmentWriter类:

class XmlFragmentWriter : XmlTextWriter

类XmlFragmentWriter:XmlTextWriter的

{

{

    public XmlFragmentWriter(TextWriter w) : base(w){}

公共XmlFragmentWriter(TextWriter w):基本(w){}

    public XmlFragmentWriter(Stream w, Encoding encoding) : base(w, encoding) {}

public XmlFragmentWriter(Stream w,编码编码): base (w,编码){}

    public XmlFragmentWriter(string filename, Encoding encoding) :         base(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None), encoding){}

公共XmlFragmentWriter(字符串文件名,编码编码):基本(新的FileStream(文件名,FileMode.Create,FileAccess.Write,FileShare.None),编码){}

    bool _skip = false;

bool _skip = false ;

    public override void WriteStartAttribute( string prefix, string localName, string ns )

公共重写void WriteStartAttribute(字符串前缀,字符串localName,字符串ns)

    {

{

        // STEP 1 - Omits XSD and XSI declarations.

//步骤1-省略XSD和XSI声明。

        // From Kzu - http://weblogs.asp.net/cazzu/archive/2004/01/23/62141.aspx

//来自Kzu-http://weblogs.asp.net/cazzu/archive/2004/01/23/62141.aspx

        if ( prefix == "xmlns" && ( localName == "xsd" || localName == "xsi" ) )  

if (前缀==“ xmlns” &&(localName ==“ xsd” || localName ==“ xsi”))

        {

{

            _skip = true;

_skip = true ;

            return;

回报;

        }

}

        base.WriteStartAttribute( prefix, localName, ns );

base .WriteStartAttribute(前缀,localName,ns);

    }

}

    public override void WriteString( string text )

公共重写void WriteString( string text)

    {

{

        if ( _skip ) return;

如果(_skip)返回;

        base.WriteString( text );

base .WriteString(text);

    }

}

    public override void WriteEndAttribute()

公共重写void WriteEndAttribute()

    {

{

        if ( _skip )

如果(_skip)

        {

{

            // Reset the flag, so we keep writing.

//重置标志,因此我们继续写。

            _skip = false;

_skip = false ;

            return;

回报;

        }

}

        base.WriteEndAttribute();

base .WriteEndAttribute();

    }

}

    public override void WriteStartDocument()

公共重写void WriteStartDocument()

    {

{

       // STEP 2: Do nothing so we omit the xml declaration.

//步骤2:不执行任何操作,因此我们省略了xml声明。

    }

}

}

}

Thanks Kzu and Sairama for giving me these pieces to assemble. I tell you, System.Xml is slick slick slick. Updated with a cleaner solution.

感谢Kzu和Sairama给我这些组装件。 我告诉你,System.Xml是光滑的。 更新了更干净的解决方案。

(You can also get rid of the namespaces with another trick but it smells hacky and I don't know what the side effects would be if your document had other namespaces)

(您还可以使用另一种技巧摆脱名称空间,但它闻起来很hacky,而且我不知道如果您的文档具有其他名称空间会带来什么副作用)

Now playing: Akon - Show Out

现在播放: Akon-展示

翻译自: https://www.hanselman.com/blog/xmlfragmentwriter-omiting-the-xml-declaration-and-the-xsd-and-xsi-namespaces

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值