简体   繁体   English

C#对象序列化到XML名称空间和仅在子元素中的前缀

[英]C# Object Serialization to XML namespace and prefix in child element only

How to get a namespace set at a child level element when serializing an object to XML with C#? 使用C#将对象序列化为XML时,如何在子级元素上获取名称空间集?

The XML I want to end up with this as the XML output: 我想以此结束的XML作为XML输出:

<clsPerson>
  <FirstName>Jeff</FirstName>
  <MI>J</MI>
  <LastName>Jefferson</LastName>
  <ns1:Addresses xmlns="www.whatever.co.uk">
    <Home>Here</Home>
    <Work>There</Work>
  </Addresses>
</clsPerson>

The code I'm using is mostly taken from other questions, and is as follows: 我使用的代码主要来自其他问题,如下所示:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class clsPerson
{
   public string FirstName;
   public string MI;
   public string LastName;

   [XmlElement(Namespace = "www.whatever.co.uk")]
   public clsPlaces Addresses; 

       public clsPerson()
       {
         clsPlaces clsPlaces = new clsPlaces();
       }
 }

[XmlRoot(Namespace = "")]
public class clsPlaces
{
public string Home { get; set; }
public string Work { get; set; }
   }

class class1
{
 static void Main(string[] args)
{
    clsPerson p = new clsPerson();
    var xml = "";
    p.FirstName = "Jeff";
    p.MI = "J";
    p.LastName = "Jefefrson";
    p.Addresses = new clsPlaces();
    p.Addresses.Home = "Here";
    p.Addresses.Work = "There";

    var emptyNamepsaces = new XmlSerializerNamespaces(new[] {       XmlQualifiedName.Empty });
    var serializer = new XmlSerializer(p.GetType());
    var settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;

   emptyNamepsaces.Add("ns", "www.whatever.co.uk");

    using (var stream = new StringWriter())
    using (var writer = XmlWriter.Create(stream, settings))
    {
        serializer.Serialize(writer, p, emptyNamepsaces);
        xml =  stream.ToString();
    }

       Console.WriteLine(xml)
       Console.ReadLine();
    }
}

The above gives: 上面给出:

<clsPerson xmlns:ns="www.whatever.co.uk">
  <FirstName>Jeff</FirstName>
  <MI>J</MI>
  <LastName>Jefefrson</LastName>
  <ns:Addresses>
    <Home>Here</Home>
    <Work>There</Work>
  </ns:Addresses>

How would I get the xmlns on the element only? 如何仅在元素上获取xmlns? I have tried everything I can see search stackoverflow, and not been able to find/understand the answer. 我已经尝试了所有我可以看到的搜索stackoverflow,但无法找到/理解答案。

Thanks 谢谢

Remove this - adding the namespace will ensure the prefix gets added to the root: 删除它-添加名称空间将确保将前缀添加到根目录:

emptyNamepsaces.Add("ns", "www.whatever.co.uk");

The declaration for addresses will get added automatically. 地址声明将自动添加。 You also need to remove the empty namespace from clsPlaces , else the namspaces for Home and Work will be incorrect: 您还需要从clsPlaces删除空的命名空间,否则HomeWork命名空间将不正确:

public class clsPlaces
{
    public string Home { get; set; }
    public string Work { get; set; }
}

The output will then look like this: 输出将如下所示:

<clsPerson>
  <FirstName>Jeff</FirstName>
  <MI>J</MI>
  <LastName>Jefefrson</LastName>
  <Addresses xmlns="www.whatever.co.uk">
    <Home>Here</Home>
    <Work>There</Work>
  </Addresses>
</clsPerson>

See this fiddle for a demo. 看到这个小提琴演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM