简体   繁体   English

XML Serialization 格式需要非常具体,如何序列化与父类同名的子类?

[英]XML Serialization format needs to be very specific, how do you serialize child classes that have the same name as their parent class?

I'm fairly new to programming and need to figure out how to serialize an object so that the XML file has the format shown below.我对编程还很陌生,需要弄清楚如何序列化一个对象,以便 XML 文件具有如下所示的格式。

<?xml version="1.0" encoding="utf-16"?>
<OpenModelResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ResultOnMembers>
    <ResultOnMembers>
      <Members>
        <ResultOnMember>
          <Member>
            <MemberType>Member1D</MemberType>
            <Id>1</Id>
          </Member>
          <ResultType>InternalForces</ResultType>
          <Results>
            <ResultBase xsi:type="ResultOnSection">
              <AbsoluteRelative>Absolute</AbsoluteRelative>
              <Position>0</Position>
              <Results>
                <SectionResultBase xsi:type="ResultOfInternalForces">
                  <Loading>
                    <LoadingType>LoadCase</LoadingType>
                    <Id>1</Id>
                    <Items>
                      <ResultOfLoadingItem>
                        <Coefficient>1</Coefficient>
                      </ResultOfLoadingItem>
                    </Items>
                  </Loading>
                  <N>242.96484375</N>
                  <Qy>0</Qy>
                  <Qz>1176.9375</Qz>
                  <Mx>0</Mx>
                  <My>-727.5482177734375</My>
                  <Mz>0</Mz>
                </SectionResultBase>
                <SectionResultBase xsi:type="ResultOfInternalForces">
                  <Loading>
                    <LoadingType>LoadCase</LoadingType>
                    <Id>2</Id>
                    <Items>
                      <ResultOfLoadingItem>
                        <Coefficient>1</Coefficient>
                      </ResultOfLoadingItem>
                    </Items>
                  </Loading>
                  <N>20200.224609375</N>
                  <Qy>0</Qy>
                  <Qz>97851.2109375</Qz>
                  <Mx>0</Mx>
                  <My>-60488.7421875</My>
                  <Mz>0</Mz>
                </SectionResultBase>
              </Results>
            </ResultBase>
          <Results>
        </ResultOnMember>
      <Members>
    <ResultOnMembers>
  <ResultOnMembers>
</OpenModelResult>

I currently have something like this but obviously, all the nodes are going to be a child of the OpenModelResult parent.我目前有类似的东西,但显然,所有节点都将成为 OpenModelResult 父节点的子节点。 Can someone point me in the right direction on how to do this?有人可以指出我如何做到这一点的正确方向吗?

public class OpenModelResult
{
    public string ResultonMembers { get; set; }
    public string Members { get; set; }
    public string ResultonMember { get; set; }
    public string Member { get; set; }
    public string MemberType { get; set; }
    public string Id { get; set; }
    public string Results { get; set; }
    public string ResultBase { get; set; }
    public string AbsoluteRelative { get; set; }
    public string Position { get; set; }
    public string SectionResultBase { get; set; }
    public string Loading { get; set; }
    public string LoadingType { get; set; }
    public string Items { get; set; }
    public string ResultOfLoadingItem { get; set; }
    public string Coefficient { get; set; }
    public double N { get; set; }
    public double Qy { get; set; }
    public double Qz { get; set; }
    public double Mx { get; set; }
    public double My { get; set; }
    public double Mz { get; set; }

    public void Save(string fileName)
    {
        using (var stream = new FileStream(fileName, FileMode.Create))
        {
            XmlSerializer XML = new XmlSerializer(typeof(OpenModelResult));
            XML.Serialize(stream, this);
        }
    }
}

Here is the start of the code using Xml Linq.这是使用 Xml Linq 的代码的开始。 You already have results ia single class and I think xml linq would be better than xml serialization你已经有了单个类的结果,我认为 xml linq 会比 xml 序列化更好

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            OpenModelResult result = new OpenModelResult();
            OpenModelResult.results.Add(new OpenModelResult());  // added to test code
            result.Save(FILENAME);
        }

    }
    public class OpenModelResult
    {
        public static List<OpenModelResult> results = new List<OpenModelResult>();
        public string ResultonMembers { get; set; }
        public string Members { get; set; }
        public string ResultonMember { get; set; }
        public string Member { get; set; }
        public string MemberType { get; set; }
        public string Id { get; set; }
        public string Results { get; set; }
        public string ResultBase { get; set; }
        public string AbsoluteRelative { get; set; }
        public string Position { get; set; }
        public string SectionResultBase { get; set; }
        public string Loading { get; set; }
        public string LoadingType { get; set; }
        public string Items { get; set; }
        public string ResultOfLoadingItem { get; set; }
        public string Coefficient { get; set; }
        public double N { get; set; }
        public double Qy { get; set; }
        public double Qz { get; set; }
        public double Mx { get; set; }
        public double My { get; set; }
        public double Mz { get; set; }

        public void Save(string fileName)
        {
            string ident = 
                "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + 
                "<OpenModelResult" + 
                   " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" + 
                   " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                "</OpenModelResult>";

            XDocument doc = XDocument.Parse(ident);
            XElement xOpenModelResult = doc.Root;
            XNamespace nsXsi = xOpenModelResult.GetNamespaceOfPrefix("xsi");

            XElement xResultOnMembers = new XElement("ResultOnMembers");
            xOpenModelResult.Add(xResultOnMembers);

            XElement xMembers = new XElement("Members");
            xResultOnMembers.Add(xMembers);

            foreach (OpenModelResult result in results)
            {
                XElement xResultOnMember = new XElement("ResultOnMember");
                xMembers.Add(xResultOnMember);

                XElement xMember = new XElement("Member", new object[] {
                    new XElement("MemberType",result.MemberType),
                    new XElement("Id",result.Id),
                });
                xResultOnMember.Add(xMember);

                xResultOnMember.Add(new XElement("ResultType", "InternalForces"));

                XElement xResults = new XElement("Results");
                xResultOnMember.Add(xResults);

                XElement xResultBase = new XElement("ResultBase", new XAttribute(nsXsi + "type", "ResultOnSection"));
                xResults.Add(xResultBase);
 


            }
            doc.Save(fileName);


        }
    }
 
}

暂无
暂无

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

相关问题 XML序列化:序列化自同一基类派生的自定义类列表,并使用元素的基类名称 - Xml serialization: serialize list of custom classes, derived from the same base class, using base class name for elements 如何使不同子类的实例具有其共同父类 class 的相同实例? - How do I make instances of different child classes have the same instance of their common parent class? XML序列化:如何区分使用相同元素名称但在属性中具有不同值的类? - XML Serialization: How to distinguish classes that use the same element name but have a different value in an attribute? 家长班需要知道他的孩子班 - A parent class needs to know his child classes C#Xml序列化:将类属性序列化为父类的属性 - C# Xml Serialization: Serialize Class properties as attributes to the parent class 当XML父节点和子节点具有相同的名称时,如何使用LINQ to XML - How to use LINQ to XML when XML parent and child nodes have the same name 如何使用C#序列化序列化为非常自定义的格式 - How to use C# serialization to serialize to a very custom format 属性相同,但类名不同-XML序列化 - Same properties but different class name - XML Serialization 如何在c#中读取与父级同名的xml节点子级 - How do I read an xml node child that has same name as parent in c# Xml序列化:无法在父元素和子元素中定义相同的名称空间 - Xml Serialization: Can't define same namespace in parent and child element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM