XMLSerialization - 自定义根属性

时间:2018-04-09 19:42:01

标签: c# .net xmlserializer

我正在开发一个序列化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">

root有两个不同的命名空间,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,还是有不同的方法来添加此属性?

1 个答案:

答案 0 :(得分:1)

我认为这可能会满足您的需求。你在那里,:)。

你的班级:

[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);
        }
    }