• xml去除头部


    /// <summary>
    /// 去除头部xml头
    /// </summary>
    public class XmlMessageSerializer
    {
    public static string SerializeToXML(Object obj)
    {
    string outXML = string.Empty;
    if (obj == null)
    return outXML;
    XmlSerializer xs = new XmlSerializer(obj.GetType(),
    new XmlRootAttribute("input"));
    //namsepaces is emty
    //to remove xmlns <xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] {
    new XmlQualifiedName(string.Empty, string.Empty) // Default Namespace
    });
    // I'll use a MemoryStream as my backing store.
    using (MemoryStream ms = new MemoryStream())
    {
    // This is extra! If you want to change the settings for the XmlSerializer, you have to create
    // a separate XmlWriterSettings object and use the XmlTextWriter.Create(...) factory method.
    // So, in this case, I want to omit the XML declaration.
    XmlWriterSettings xws = new XmlWriterSettings();
    xws.OmitXmlDeclaration = true;
    xws.Encoding = Encoding.UTF8; // This is probably the default
    //equal writer.Formatting = Formatting.Indented;
    xws.Indent = true;
    var xwr = XmlTextWriter.Create(ms, xws);
    // remove <?xml header
    //http://stackoverflow.com/questions/7913798/xmlserializer-to-xelement
    ms.Position = 0;
    xs.Serialize(xwr, obj, namespaces);

    outXML = System.Text.Encoding.UTF8.GetString(ms.ToArray());
    }
    return outXML;
    }
    public T DeSeriralze<T>(string xmlStr)
    {
    XmlSerializer xmS = new XmlSerializer(typeof(T));
    object recoveryObject = null;
    StringReader sr = null;
    try
    {
    sr = new StringReader(xmlStr);
    //默认用UTF-8打开文件
    recoveryObject = xmS.Deserialize(sr);
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    if (sr != null)
    sr.Close();
    }
    return (T)recoveryObject;
    }
    }

  • 相关阅读:
    GRUB引导界面背景图片制作完整教程
    git遇到问题 Flandre
    NOIP2021 比赛记录 Flandre
    一个不错的回车提交按钮
    防止SQL注入
    NLog 不能些日志
    ajax 加载partial view ,并且 附加validate验证
    linq 常用查询
    Android开发工具问题之ADTversion
    C# 实现一个简单的FTP服务器
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/16103666.html
Copyright © 2020-2023  润新知