c#xml serialization将命名空间添加到节点

时间:2017-01-14 17:37:05

标签: c# xml

我有两个课:Osoba,考试

public class test
{

    public string raz { get; set; }
    public string dwa { get; set; }
}

public class Osoba
{
    public test tehe { get; set; }
}

我还将命名空间添加到主根并且seralize

        Osoba ne = new Osoba();
        test t1 = new praca2.test();


        t1.dwa = "fgfg";
        t1.raz = "dfdfdfdf";
        ne.tehe = t1;            
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
        ns.Add("d", "http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields");
        ns.Add("pc", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");

        XmlSerializer xsSubmit = new XmlSerializer(typeof(Osoba));
        var xml = @"D:\dupa1.xml";

        using (var stream = new FileStream(xml, FileMode.Create))
        {
            using (XmlWriter writer = XmlWriter.Create(stream))
            {
                xsSubmit.Serialize(writer, ne,ns);
                xml = stream.ToString(); // Your XML
            }
        }

我得到了

<?xml version="1.0" encoding="utf-8"?>
<Osoba xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields">
 <tehe>
    <raz>dfdfdfdf</raz>
    <dwa>fgfg</dwa>
 </tehe>
</Osoba>

我想添加节点名称空间检查:

...
  <pc:tehe>
     <dfs:raz>dfdfdfdf</dfs:raz>
     <dfs:dwa>fgfg</dfs:dwa>
  </pc:tehe>

我怎么做? 我尝试添加设置名称空间

的类属性
   [XmlRoot("Node", Namespace="http://flibble")] 

但不好主意

3 个答案:

答案 0 :(得分:0)

你几乎就在那里,你只需稍微修改你的课程:

    public class test
    {
        [XmlElement(Namespace = "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution")]
        public string raz { get; set; }
        [XmlElement(Namespace = "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution")]
        public string dwa { get; set; }
    }

    public class Osoba
    {
        [XmlElement(Namespace = "http://schemas.microsoft.com/office/infopath/2007/PartnerControls")]
        public test tehe { get; set; }
    }

主要从您自己复制的示例实现:

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

            Osoba ne = new Osoba();
            test t1 = new test();

            t1.dwa = "fgfg";
            t1.raz = "dfdfdfdf";
            ne.tehe = t1;

            XmlSerializer xsSubmit = new XmlSerializer(typeof(Osoba));

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
            ns.Add("pc", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
            var xml = @"D:\dupa1.xml";


            using (var stream = new FileStream(xml, FileMode.Create))
            {
                using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                {
                    xsSubmit.Serialize(writer, ne, ns);
                }
            }

        }
    }

您将获得此XML:

<?xml version="1.0" encoding="utf-8"?>
<Osoba xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls">
    <pc:tehe>
        <dfs:raz>dfdfdfdf</dfs:raz>
        <dfs:dwa>fgfg</dfs:dwa>
    </pc:tehe>
</Osoba>

... H个

答案 1 :(得分:0)

Uisng xml linq:

{{1}}

答案 2 :(得分:-1)

我认为您是C#的新手。我建议您学习C#基础知识,设计模式和存储库模式。根据您的要求,您可以使用以下代码

public class Tehe
{
    public string Raz { get; set; }
    public string Dwa { get; set; }
}

public class TeheRepository
{
    private System.Xml.Linq.XDocument xmlDatas;

    public TeheRepository(string filePath)
    {
        xmlDatas = XDocument.Load(filePath);
    }

    public IEnumerable<Tehe> FindAll()
    {
        return xmlDatas.Elements().Elements().Select(tehe => 
        {
            Tehe t = new Tehe();
            t.Dwa = tehe.Elements().ElementAt(1).Value;
            t.Raz = tehe.Elements().ElementAt(0).Value;
            return t;
        });
    }
}