.net 通过获取示例XML创建动态XML文件

xxls0lw8  于 2023-02-20  发布在  .NET
关注(0)|答案(1)|浏览(127)

我必须以XML为例创建一个动态XML。
例如:

<root>
     <cde>
          <id1>11</id1>
          <id2>aa3</id2>
          <listProducts>
                <Products>
                     <id>123123</ndg>
                     <Name>AAFFF</Name>
                     <listProductsService>
                          <ProductsService>
                               <id>AA22</id>
                               <numRapp>324554</numRapp>
                          </ProductsService>
                     </listProductsService>
                     <listProcess>
                          <idProcessItem>FDD223</idProcessItem>
                     </listProcess>
                </Products>
          </listProducts>
          <ddd>DSSVDDSS</ddd>
          <dateVar>2022/02/22 12:15:00</dateVar>
     </cde>
     <fgh>
          <id1>AB223</id1>
          <idDoc>AACC4454</idDoc>
          <idAAA>CCCVV223</idAAA>
          <progrVers>1</progrVers>
          <listCF>
                <DescrCF>
                     <id>123456</id>
                     <descr>VVVV</descr>
                     <cgggg>AAAAA</cgggggg>
                </DescrCF>
          </listCF>
          <bbbffd>VVVVV</bbbffd>
          <hhhggg>DDDDDDDD</hhhggg>
     </fgh>
     <cde>
          <id1>55</id1>
          <id2>bbff3</id2>
          <listProducts>
                <Products>
                     <id>123123</ndg>
                     <Name>AAFFF</Name>
                     <listProductsService>
                          <ProductsService>
                               <id>AA22</id>
                               <numRapp>324554</numRapp>
                          </ProductsService>
                     </listProductsService>
                     <listProcess>
                          <idProcessItem>FDD223</idProcessItem>
                     </listProcess>
                </Products>
          </listProducts>
          <ddd>DSSVDDSS</ddd>
          <dateVar>2022/02/22 12:15:00</dateVar>
     </cde>
     <fgh>
          <id>FFFVVGG332</id>
          <idDoc>FFC33</idDoc>
          <idAAA>CCCVV223</idAAA>
          <progrVers>1</progrVers>
          <listCF>
                <DescrCF>
                     <id>123456</id>
                     <descr>VVVV</descr>
                     <cgggg>AAAAA</cgggggg>
                </DescrCF>
          </listCF>
          <bbbffd>VVVVV</bbbffd>
          <hhhggg>DDDDDDDD</hhhggg>
     </fgh>
</root>

我必须循环<cde><fgh>以获取所有数据。如何通过循环它们来动态创建XML?root必须只有一个。
测试:<cde><fgh>的id1必须在xml文件中匹配。
我试着创建一个dto模型,然后循环数据,但没有任何效果。我试着创建一个虚拟的JSON,但没有任何效果。你有什么建议吗?
我试着用所有的数据创建一个类:

public class root
{
  public cde cde { get; set; }
  public fgh fgh { get; set; }
}

public class cde 
{
  public string id1 { get; set; }
  public string id2 { get; set; }
  public listProducts { get; set; }
}
....

循环数据:

var root = new List<Root>();

for(int i = 0; i < data.Count(); i++)
{
  root.Add(root = new root
  {
    cde = new cde {
     id1 = 11,
     id2 = 222,
     listProducts = new listProducts {
       listProducts = new listProducts 
       {
        ....
       }
     }
    }
  }
}

作为最终结果,我得到一个列表。
为了创建XML,我依赖于XMLSerialize:

var settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
Async = false
}

var ns = new XmlSerializerNamespaces(new[] {XmlQualifiedName.Empty});

Xmlserializer x = new Xmlserializer(root.GetType());

using(var writer = XmlWriter.Create(@"c:\test.xml", settings)
{

  x.Serialize(writer, root, ns);
}

但是我的xml文件看起来像这样:

<ArrayOfRoots>
  <root>
     <cde>
          <id1>11</id1>
          <id2>aa3</id2>
          <listProducts>
                <Products>
                     <id>123123</ndg>
                     <Name>AAFFF</Name>
                     <listProductsService>
                          <ProductsService>
                               <id>AA22</id>
                               <numRapp>324554</numRapp>
                          </ProductsService>
                     </listProductsService>
                     <listProcess>
                          <idProcessItem>FDD223</idProcessItem>
                     </listProcess>
                </Products>
          </listProducts>
          <ddd>DSSVDDSS</ddd>
          <dateVar>2022/02/22 12:15:00</dateVar>
     </cde>
     <fgh>
          <id1>AB223</id1>
          <idDoc>AACC4454</idDoc>
          <idAAA>CCCVV223</idAAA>
          <progrVers>1</progrVers>
          <listCF>
                <DescrCF>
                     <id>123456</id>
                     <descr>VVVV</descr>
                     <cgggg>AAAAA</cgggggg>
                </DescrCF>
          </listCF>
          <bbbffd>VVVVV</bbbffd>
          <hhhggg>DDDDDDDD</hhhggg>
     </fgh>
     <cde>
          <id1>55</id1>
          <id2>bbff3</id2>
          <listProducts>
                <Products>
                     <id>123123</ndg>
                     <Name>AAFFF</Name>
                     <listProductsService>
                          <ProductsService>
                               <id>AA22</id>
                               <numRapp>324554</numRapp>
                          </ProductsService>
                     </listProductsService>
                     <listProcess>
                          <idProcessItem>FDD223</idProcessItem>
                     </listProcess>
                </Products>
          </listProducts>
          <ddd>DSSVDDSS</ddd>
          <dateVar>2022/02/22 12:15:00</dateVar>
     </cde>
     <fgh>
          <id>FFFVVGG332</id>
          <idDoc>FFC33</idDoc>
          <idAAA>CCCVV223</idAAA>
          <progrVers>1</progrVers>
          <listCF>
                <DescrCF>
                     <id>123456</id>
                     <descr>VVVV</descr>
                     <cgggg>AAAAA</cgggggg>
                </DescrCF>
          </listCF>
          <bbbffd>VVVVV</bbbffd>
          <hhhggg>DDDDDDDD</hhhggg>
     </fgh>
</root>
<root>
     <cde>
          <id1>11</id1>
          <id2>aa3</id2>
          <listProducts>
                <Products>
                     <id>123123</ndg>
                     <Name>AAFFF</Name>
                     <listProductsService>
                          <ProductsService>
                               <id>AA22</id>
                               <numRapp>324554</numRapp>
                          </ProductsService>
                     </listProductsService>
                     <listProcess>
                          <idProcessItem>FDD223</idProcessItem>
                     </listProcess>
                </Products>
          </listProducts>
          <ddd>DSSVDDSS</ddd>
          <dateVar>2022/02/22 12:15:00</dateVar>
     </cde>
     <fgh>
          <id1>AB223</id1>
          <idDoc>AACC4454</idDoc>
          <idAAA>CCCVV223</idAAA>
          <progrVers>1</progrVers>
          <listCF>
                <DescrCF>
                     <id>123456</id>
                     <descr>VVVV</descr>
                     <cgggg>AAAAA</cgggggg>
                </DescrCF>
          </listCF>
          <bbbffd>VVVVV</bbbffd>
          <hhhggg>DDDDDDDD</hhhggg>
     </fgh>
     <cde>
          <id1>55</id1>
          <id2>bbff3</id2>
          <listProducts>
                <Products>
                     <id>123123</ndg>
                     <Name>AAFFF</Name>
                     <listProductsService>
                          <ProductsService>
                               <id>AA22</id>
                               <numRapp>324554</numRapp>
                          </ProductsService>
                     </listProductsService>
                     <listProcess>
                          <idProcessItem>FDD223</idProcessItem>
                     </listProcess>
                </Products>
          </listProducts>
          <ddd>DSSVDDSS</ddd>
          <dateVar>2022/02/22 12:15:00</dateVar>
     </cde>
     <fgh>
          <id>FFFVVGG332</id>
          <idDoc>FFC33</idDoc>
          <idAAA>CCCVV223</idAAA>
          <progrVers>1</progrVers>
          <listCF>
                <DescrCF>
                     <id>123456</id>
                     <descr>VVVV</descr>
                     <cgggg>AAAAA</cgggggg>
                </DescrCF>
          </listCF>
          <bbbffd>VVVVV</bbbffd>
          <hhhggg>DDDDDDDD</hhhggg>
     </fgh>
</root>
</arrayofroots>

把所有的根相乘,但我只需要一个根和几个和

6tdlim6h

6tdlim6h1#

您的xml不是动态的,它具有已知的格式。当XML文件具有架构时,最好使用xsd.exe创建c#类并使用xml序列化。但这种方法速度慢,通常会导致类具有许多层,并且由于深度层的数量而使数据处理变得更困难。
我的方法是尝试将数据扁平化并使用XMLLinq(XDocument)来解析数据。下面是我将用于您的XML的类。包含“list”的XML标记似乎不是真正的列表,而是来自所提供示例的单例,但可能真的是列表。

public class Roots
    {
        public List<Root> roots { get; set; }
    }
    public class Root
    {
        public List<CDE> cde { get; set; }
        public List<FGH> fgh { get; set; }
    }
    public class CDE
    {
        public int id1 { get; set; }
        public string id2 { get; set; }
        public int productID { get; set; }
        public string productName { get; set; }
        public string serviceID { get; set; }
        public string numRapp { get; set; }
        public string idProcessItem { get; set; }
    }
    public class FGH
    {
        public string id1 { get; set; }
        public string idDoc { get; set; }
        public string idAAA { get; set; }
        public int progrVers { get; set; }
        public int productId { get; set; }
        public string productName { get; set; }
        public string productService { get; set; }
        public string serviceId { get; set; }
        public string numRapp { get; set; }
        public string idProcessItem { get; set; }

    }

相关问题