XMLSerialization - 自定义根属性

XMLSerialization - Custom root attributes

我正在开发一个序列化 XML 并将其设置为 Web 服务的项目。根元素是高度自定义的,看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<eAction xmlns="http://www.action.org/standards/PC_Surety/action1/xml/" xmlns:cation="http://www.caction.org/standards/pc_go/xml/" xmlns:acme="http://www.ACME.org/standards/ACME1/xml/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

根有几个不同的命名空间,xsd 属性不存在。

我一直在努力研究如何添加额外的命名空间属性,如果序列化不是解决此问题的方法。

到目前为止,我有一个测试程序,其中包含一组简单的 类 序列化:

private void button1_Click(object sender, EventArgs e)
{
  XmlSerializer xmlSerialize = new XmlSerializer(typeof(eAction));
  TextWriter txtWriter = new StreamWriter(@"c:\actionout.xml");
  eAction eActionItem = new eAction();

  xmlSerialize.Serialize(txtWriter, eActionItem);
}

[XmlRoot(ElementName="eAction")]
public class eAction
{
  public string Name = string.Empty;
  public string Street1 = string.Empty;
  public string Street2 = string.Empty;
  public string City = string.Empty;
  public string State = string.Empty;
  public string PostalCode = string.Empty;

  public OtherInformation OtherInformation = new OtherInformation();
}

public class OtherInformation
{
  public string DateOfBirth = string.Empty;
  public string SSN = string.Empty;
}

运行 这创建了这个 xml:

<?xml version="1.0" encoding="utf-8"?>
<eAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name />
  <Street1 />
  <Street2 />
  <City />
  <State />
  <PostalCode />
  <OtherInformation>
    <DateOfBirth />
    <SSN />
  </OtherInformation>
</eAction>

有什么好的方法可以添加我需要的属性吗?

编辑:接受的答案解决了问题。还有一个问题-我还要补充:

xsi:schemaLocation="http://www.action.org/standards/PC_Surety/action1/xml/ standardsFile.xsd"

是否可以将其添加为命名空间,但用 xsi 代替 xmlns,还是有不同的方法来添加此属性?

我认为这可能会满足您的需要。你是对的,:) .

你的class:

[XmlRoot(ElementName = "eAction", Namespace = "http://www.action.org/standards/PC_Surety/action1/xml/")]
public class eAction
{
    [XmlAttribute(Namespace = XmlSchema.InstanceNamespace)]
    public string schemaLocation = "http://www.action.org/standards/PC_Surety/action1/xml/standardsFile.xsd";
    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

    public eAction()
    {
        xmlns.Add("cation", "http://www.caction.org/standards/pc_go/xml/");
        xmlns.Add("acme", "http://www.ACME.org/standards/ACME1/xml/");
        xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    }

    public string Name = string.Empty;
    public string Street1 = string.Empty;
    public string Street2 = string.Empty;
    public string City = string.Empty;
    public string State = string.Empty;
    public string PostalCode = string.Empty;

    public OtherInformation OtherInformation = new OtherInformation();
}

public class OtherInformation
{
    public string DateOfBirth = string.Empty;
    public string SSN = string.Empty;
}

使用示例:

        private static void button1_click()
    {
        var xmlSerializer = new XmlSerializer(typeof(eAction));
        using (var txtWriter = new StreamWriter(@"C:\temp\actionout.xml"))
        {
            var eActionItem = new eAction();
            xmlSerializer.Serialize(txtWriter, eActionItem);
        }
    }