Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#类转换为XML,其中显示的条目列表中没有“";“父亲财产”;节点_C#_Xml - Fatal编程技术网

C#类转换为XML,其中显示的条目列表中没有“";“父亲财产”;节点

C#类转换为XML,其中显示的条目列表中没有“";“父亲财产”;节点,c#,xml,C#,Xml,具有以下示例代码: public class MyHeader { public string Identification { get; set; } public DateTime Date { get; set; } public string Description { get; set; } public List<MyChild> Children { get; set; } } public class MyChild { p

具有以下示例代码:

public class MyHeader
{
    public string Identification { get; set; }
    public DateTime  Date { get; set; }
    public string Description { get; set; }
    public List<MyChild> Children { get; set; }
}

public class MyChild
{
    public string Identification { get; set; }
    public string Description { get; set; }
}

public string GetXML()
    {
        var h = new MyHeader
        {
            Date = DateTime.Now,
            Description = "Something",
            Identification = "AAAAA11111",
            Children = new List<MyChild>
            {
                new MyChild { Identification = "B1", Description = "B1"},
                new MyChild { Identification = "B2", Description = "B2"},
                new MyChild { Identification = "B3", Description = "B3"}
            }
        };
        var xml = "";
        var serializer = new XmlSerializer(h.GetType());
        using (var sww = new StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                serializer.Serialize(writer, h);
                xml = sww.ToString();
            }
        }
        return xml;
    }
公共类MyHeader
{
公共字符串标识{get;set;}
公共日期时间日期{get;set;}
公共字符串说明{get;set;}
公共列表子项{get;set;}
}
公共类MyChild
{
公共字符串标识{get;set;}
公共字符串说明{get;set;}
}
公共字符串GetXML()
{
var h=新的MyHeader
{
日期=日期时间。现在,
Description=“某物”,
Identification=“aaaa11111”,
儿童=新列表
{
新MyChild{Identification=“B1”,Description=“B1”},
新建MyChild{Identification=“B2”,Description=“B2”},
新MyChild{Identification=“B3”,Description=“B3”}
}
};
var xml=“”;
var serializer=新的XmlSerializer(h.GetType());
使用(var sww=new StringWriter())
{
使用(XmlWriter=XmlWriter.Create(sww))
{
serializer.Serialize(writer,h);
xml=sww.ToString();
}
}
返回xml;
}
结果是:

<?xml version="1.0" encoding="utf-16"?>
<MyHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Identification>AAAAA11111</Identification>
    <Date>2020-09-09T11:55:04.2457349+02:00</Date>
    <Description>Something</Description>
    <Children>
        <MyChild>
            <Identification>B1</Identification>
            <Description>B1</Description>
        </MyChild>
        <MyChild>
            <Identification>B2</Identification>
            <Description>B2</Description>
        </MyChild>
        <MyChild>
            <Identification>B3</Identification>
            <Description>B3</Description>
        </MyChild>
    </Children>
</MyHeader>

AAAA 11111
2020-09-09T11:55:04.2457349+02:00
某物
地下一层
地下一层
地下二层
地下二层
地下三层
地下三层
现在的问题是:在不显示
节点的情况下生成最终XML,但在标题中合并子节点的最简单解决方案是什么

我正在寻找的结果:

<?xml version="1.0" encoding="utf-16"?>
<MyHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Identification>AAAAA11111</Identification>
    <Date>2020-09-09T11:55:04.2457349+02:00</Date>
    <Description>Something</Description>
    <MyChild>
        <Identification>B1</Identification>
        <Description>B1</Description>
    </MyChild>
    <MyChild>
        <Identification>B2</Identification>
        <Description>B2</Description>
    </MyChild>
    <MyChild>
        <Identification>B3</Identification>
        <Description>B3</Description>
    </MyChild>
</MyHeader>

AAAA 11111
2020-09-09T11:55:04.2457349+02:00
某物
地下一层
地下一层
地下二层
地下二层
地下三层
地下三层

在列表声明上方添加一个
xmlementAttribute

[XmlElement(ElementName = "MyChild")]
public List<MyChild> Children { get; set; }
[xmlement(ElementName=“MyChild”)]
公共列表子项{get;set;}
这将在父对象
MyHeader
中将每个子对象视为自己的项