本文介绍了如何动态更改 XmlElement 名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[XmlRoot("A")]
public class Foos
{
   [XmlElement("A1")]
   public List<Foo> FooList{ get; set; }
}


var serializer = new XmlSerializer(typeof(Foos));

此代码也能正常工作.但它不是动态的.我想要 [XmlRoot("A")][XmlRoot(ConfigurationManager.AppSettings[someValue])].但是会抛出语法错误.然后我试试这个

This code working as well. But its not dynamic. I want [XmlRoot("A")] to [XmlRoot(ConfigurationManager.AppSettings[someValue])]. But is throw to syntax error. Then i try this

    public class Foos
    {
       [XmlElement("A1")]
       public List<Foo> FooList{ get; set; }
    }
var serializer = new XmlSerializer(typeof(Foos),new XmlRootAttribute(ConfigurationManager.AppSettings[someValue]));

这只是根元素的工作.我正在工作.我无法动态更改 FooList 的XmlElement"值.类中可以有多个元素.如何动态更改所有这些的 XmlElement 值?

This is work just root element.I'm working. I could not change the "XmlElement" value of the FooList dynamically.There can be more than one element in the class. How do I change the XmlElement value of all of them dynamically?

推荐答案

您需要以正确的方式使用 XmlAttributesOverrides.请检查.

You need to use XmlAttributesOverrides in a right way. Please Check.

您的代码的工作版本在这里.

Working version of your code is here.

public class Foos
{       
    public List<Foo> FooList { get; set; }
}


public class Foo
{
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {

        var xmlAttributeForFoos = new XmlAttributes { XmlRoot = new XmlRootAttribute(ConfigurationManager.AppSettings["someFoosValue"]), XmlType = new XmlTypeAttribute(ConfigurationManager.AppSettings["someFoosValue"]) };
        var xmlAttributeForFooList = new XmlAttributes();
        var xmlAttributeForFoo = new XmlAttributes();

        xmlAttributeForFooList.XmlElements.Add(new XmlElementAttribute(ConfigurationManager.AppSettings["someFooValue"]));
        xmlAttributeForFoo.XmlElements.Add(new XmlElementAttribute(ConfigurationManager.AppSettings["someFooNameValue"]));

         var overrides = new XmlAttributeOverrides();
        overrides.Add(typeof(Foos), xmlAttributeForFoos);
        overrides.Add(typeof(Foos), "FooList", xmlAttributeForFooList);
        overrides.Add(typeof(Foo), "Name", xmlAttributeForFoo);

        XmlSerializer serializer = new XmlSerializer(typeof(Foos), overrides);

        var foos = new Foos
        {
            FooList = new List<Foo>
            {
                new Foo{Name = "FooName"}
            }
        };

        using (var stream = File.Open("file.xml", FileMode.OpenOrCreate))
        {
            serializer.Serialize(stream, foos);
        }
    }
}

应用设置

<appSettings>
  <add key="someFoosValue" value="SomeFoos"/>    
  <add key="someFooValue" value="SomeFoo"/>
  <add key="someFooNameValue" value="FooName"/>
</appSettings>

输出

<SomeFoos xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SomeFoo>
    <FooName>FooName</FooName>
  </SomeFoo>
</SomeFoos>

这篇关于如何动态更改 XmlElement 名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 04:07