防止在XML中编码特殊字符 [英] Prevent encoding of special characters in XML
问题描述
我开发了ac#应用程序,它从用户接收数据,然后生成XML文件并将其保存到物理路径。
< p style ="border:none; font-family:'Microsoft JhengHei UI','Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif; outline:0px; padding-right:0px; color: #333333; font-size:14px">
1. 用户在应用程序中输入字符串格式的个人信息
例如:< Name> John< / Name>< Gender> M< / Gender>
2.应用程序从数据库获取图像
3.应用程序将个人信息(UserSegment)和图像分配给对象
4. 应用程序将对象序列化为XML
我们发现特殊字符<转换为&& lt;>转换为 & gt;
我们希望输出XML像这样
< Applicant>
< UserSegment>
<名称>约翰< /名称>
<性别> M< /性别>
< / UserSegment>
<照片> ESN76By / EvzeXi8FYHH0gUakAkA + D4E =< / Photo>
< /申请人>
以下是我的代码的一部分
对象类:
私有字符串saSegmentField;
private byte [] photoField;
公共字符串SaSegment
{
get {
return this.saSegmentField;
}
set {
this.saSegmentField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(DataType =" base64Binary")]
public byte [] Photo {
get {
return this.photoField;
}
设置{
this.photoField = value;
}
}
主程序:
ApplicantDetails objAppDetails = new ApplicantDetails
{
申请人=新申请人详细申请
{
UserSegment = strUserInput,
Photo = app.Photo
}
}XmlSerializer SerializerObj = new XmlSerializer(typeof(ApplicantDetails));
如何防止XML中特殊字符的编码?
谢谢,
Tony
使用
IXmlSerializable 尝试自定义序列化:
public class ApplicantDetailsApplicant:IXmlSerializable
{
public string SaSegment {get;组; }
public byte [] Photo {get;组; }
public XmlSchema GetSchema()
{
return null;
}
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement(" UserSegment");
writer.WriteRaw(SaSegment);
writer.WriteEndElement();
writer.WriteElementString(" Photo",Convert.ToBase64String(Photo));
}
public void ReadXml(XmlReader reader)
{
throw new NotSupportedException();
}
}
它假定用户将输入正确的值。也许最好重新考虑这种方法。
I developed a c# application which receive data from user and then generate and save a XML file to a physical path.
1. User input personal information in string format in the application
e.g.: <Name>John</Name><Gender>M</Gender>
2. The application get images from database
3. The application assign personal information (UserSegment) and image to an object
4. The application serialize the object to XML
We found that special character < is converted to < and > is converted to >
We expect the output XML like this
<Applicant> <UserSegment> <Name>John</Name> <Gender>M</Gender> </UserSegment> <Photo>ESN76By/EvzeXi8FYHH0gUakAkA+D4E=</Photo> </Applicant>
The following is part of my code
Object class:
private string saSegmentField; private byte[] photoField; public string SaSegment { get { return this.saSegmentField; } set { this.saSegmentField = value; } } [System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] public byte[] Photo { get { return this.photoField; } set { this.photoField = value; } }
Main program:
ApplicantDetails objAppDetails = new ApplicantDetails { Applicant = new ApplicantDetailsApplicant { UserSegment = strUserInput, Photo = app.Photo } }
XmlSerializer SerializerObj = new XmlSerializer(typeof(ApplicantDetails));
How to prevent encoding of special characters in XML?
Thanks,
Tony
Try custom serialisation using IXmlSerializable:
public class ApplicantDetailsApplicant : IXmlSerializable { public string SaSegment { get; set; } public byte[] Photo { get; set; } public XmlSchema GetSchema() { return null; } public void WriteXml( XmlWriter writer ) { writer.WriteStartElement( "UserSegment" ); writer.WriteRaw( SaSegment ); writer.WriteEndElement(); writer.WriteElementString( "Photo", Convert.ToBase64String( Photo ) ); } public void ReadXml( XmlReader reader ) { throw new NotSupportedException(); } }
It assumes that the user will enter good values. Maybe it is better to reconsider the approach.
这篇关于防止在XML中编码特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!