如何忽略自动生成的属性并在其位置添加新属性?

时间:2015-01-19 09:17:19

标签: c# xml xml-serialization

我正在使用C#

使用序列化创建一个XML节点
<node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<childNode1><![CDATA[some file name]]></childNode1>
<childNode2><![CDATA[some file name]]></childNode2>
</node >

如何避免'node'的属性"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"并在其位置添加其他属性? 因此,结果节点应如下所示:

<node name="xyz" group="abc">

我使用的代码是:

class Program
{
    public static void Main(string[] args)
    {
        AddressDetails details = new AddressDetails();
        Serialize(details);
    }

    static public void Serialize(AddressDetails details)
    {

        XmlSerializer serializer = new XmlSerializer(typeof(AddressDetails));
        TextWriter writer = new StreamWriter(@"C:\Xml.xml");
        serializer.Serialize(writer, details);
    }
}
public class AddressDetails
{ 
    [XmlElement("Street")]
    public string StreetName { get; set; }
    [XmlElement("CityName")]
    public string City { get; set; }
    public Address address;
    public AddressDetails()
    {
        StreetName = "XYZ";
        City = "Pune";
    }
    public System.Xml.XmlCDataSection MyStringCDATA
    {
        get
        {
            return new System.Xml.XmlDocument().CreateCDataSection("load and run");
        }
        set
        {  }
    }
}
public class Address
{
    [XmlAttribute("HouseNo")]
    public int HouseNo { get; set; }
    [XmlAttribute("floor")]
    public int floor { get; set; }
    public Address()
    {
        HouseNo = 204;
        floor = 2;
    }
}

创建这个xml的目的是,我想多次创建相同的节点模板,但每个节点名称都不同,我想动态地给出节点名称。 例如。在上面的代码中,节点名称是通过类名设置的“AddressDetails”.....现在我希望节点名称为“AddressDetails_John”...“AddressDetails_Harry”..是否有任何规定来实现它以这种方式动态? -

感谢...

1 个答案:

答案 0 :(得分:0)

  1. 要禁止编写标准XML命名空间,请将空XmlSerializerNamespaces传递给XmlSerializer.Serialize()

        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
        serializer.Serialize(xmlWriter, obj, ns);
    
  2. 添加属性:当属性标记为[XmlAttribute]属性时,属性将序列化为XML属性,例如:

        [XmlAttribute("name")]
        public string Name { get; set; }
    
  3. 因此,在您的示例中,如果您希望AddressDetails.StreetAddressDetails.CityName成为属性,则可以执行以下操作:

    public class AddressDetails
    {
        [XmlAttribute("Street")]
        public string StreetName { get; set; }
    
        [XmlAttribute("CityName")]
        public string City { get; set; }
    
        public Address address;
    
        public AddressDetails()
        {
            StreetName = "XYZ";
            City = "Pune";
        }
    
        public System.Xml.XmlCDataSection MyStringCDATA
        {
            get
            {
                return new System.Xml.XmlDocument().CreateCDataSection("load and run");
            }
            set
            { }
        }
    }
    

    并且,要在序列化时省略标准名称空间,可以添加以下静态方法:

        public static void Serialize<T>(T obj, string filename, bool omitStandardNamespaces = false)  // Change the default to true if you prefer.
        {
            using (var textWriter = new StreamWriter(filename))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;        // For cosmetic purposes.
                settings.IndentChars = "    "; // For cosmetic purposes.
                using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                {
                    if (omitStandardNamespaces)
                    {
                        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                        ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
                        serializer.Serialize(xmlWriter, obj, ns);
                    }
                    else
                    {
                        serializer.Serialize(xmlWriter, obj);
                    }
                }
            }
        }