如何在每个父节点中包含一个子节点的情况下编写XML文件?

时间:2019-01-24 16:06:49

标签: c# xml xsd

我想要做的是,标题说的是,在文件具有的每个E1EDL24中写入一个E1EDL11标签。仅适用于第一个E1EDL24和E1EDL11标签。从第二个E1EDL11标签开始,下一个被写入第一个E1EDL24标签内。

这是C#中我所拥有的代码的一部分:

getchar

这是我用来读写文件的XSD文件:

foreach (var vvActualProtDetail in vvProtDetails)
{
    MstProduct vvProduct = new MstProduct()
    { 
        MstBusinessPartnerId = vvCustomerInternalCode, 
        Code = vvActualProtDetail.Product.Trim()
    };

    vvProduct.SelectNew();

    if (vvProduct.Id > 0)
    {
        vvDsSchema.Tables["E1EDL24"].Rows.Add(new object[] {
                                                                "1",
                                                                vvActualProtDetail.RowNumber.ToString().Length < 6 ? vvActualProtDetail.RowNumber.ToString().PadLeft(6, '0') : vvActualProtDetail.RowNumber.ToString(),       //POSNR
                                                                vvActualProtDetail.Product,                                             //MATNR
                                                                "CO01",                                                                 //WERKS
                                                                vvActualProtDetail.WH,                                                  //LGORT
                                                                vvActualProtDetail.Lot,                                                 //CHARG
                                                                vvActualProtDetail.QuantitySent/vvActualProtDetail.Incoming_Quantity,   //LFIMG
                                                                vvActualProtDetail.UoM,                                                 //VRKME
                                                                null,
                                                                "0"
                                                            });

        if (vvActualProtDetail.SN != null && vvActualProtDetail.SN.Trim() != "")
        {
            vvDsSchema.Tables["E1EDL11"].Rows.Add(new object[] {
                                                                    "1",
                                                                    vvActualProtDetail.SN.Trim(),   //SERNR
                                                                    "0"
                                                                });
        }
    }
}

这是我现在得到的:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="DELVRY03">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="IDOC">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="EDI_DC40">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="TABNAM" type="xs:string" />
                    <xs:element name="DIRECT" type="xs:unsignedByte" />
                    <xs:element name="IDOCTYP" type="xs:string" />
                    <xs:element name="MESTYP" type="xs:string" />
                    <xs:element name="SNDPOR" type="xs:string" />
                    <xs:element name="SNDPRT" type="xs:string" />
                    <xs:element name="SNDPRN" type="xs:string" />
                    <xs:element name="SNDSAD" type="xs:string" />
                    <xs:element name="RCVPOR" type="xs:string" />
                    <xs:element name="RCVPRT" type="xs:string" />
                    <xs:element name="RCVPRN" type="xs:string" />
                  </xs:sequence>
                  <xs:attribute name="SEGMENT" type="xs:unsignedByte" use="required" />
                </xs:complexType>
              </xs:element>
              <xs:element name="E1EDL20">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="VBELN" type="xs:string" />
                    <xs:element maxOccurs="unbounded" name="E1EDL18">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="QUALF" type="xs:string" />
                        </xs:sequence>
                        <xs:attribute name="SEGMENT" type="xs:unsignedByte" use="required" />
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="E1EDL24">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="POSNR" type="xs:string" />
                          <xs:element name="MATNR" type="xs:string" />
                          <xs:element name="WERKS" type="xs:string" />
                          <xs:element name="LGORT" type="xs:string" />
                          <xs:element name="CHARG" type="xs:string" />
                          <xs:element name="LFIMG" type="xs:decimal" />
                          <xs:element name="VRKME" type="xs:string" />
                          <xs:element name="E1EDL11">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="SERNR" type="xs:string" />
                              </xs:sequence>
                              <xs:attribute name="SEGMENT" type="xs:unsignedByte" use="required" />
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                        <xs:attribute name="SEGMENT" type="xs:unsignedByte" use="required" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="SEGMENT" type="xs:unsignedByte" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="BEGIN" type="xs:unsignedByte" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

但是实际结果必须是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<DELVRY03>
  <IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
      <TABNAM>EDI_DC40</TABNAM>
      <DIRECT>1</DIRECT>
      <IDOCTYP>DELVRY03</IDOCTYP>
      <MESTYP>SHPCON</MESTYP>
      <SNDPOR>DELLBOOMI</SNDPOR>
      <SNDPRT>LS</SNDPRT>
      <SNDPRN>3PLBOMI_CO</SNDPRN>
      <SNDSAD>NIPAS2</SNDSAD>
      <RCVPOR>SAPDE1</RCVPOR>
      <RCVPRT>LS</RCVPRT>
      <RCVPRN>DE1CLNT160</RCVPRN>
    </EDI_DC40>
    <E1EDL20 SEGMENT="1">
      <VBELN>0180347093</VBELN>
      <E1EDL18 SEGMENT="1">
        <QUALF>PGI</QUALF>
      </E1EDL18>
      <E1EDL18 SEGMENT="1">
        <QUALF>PIC</QUALF>
      </E1EDL18>
      <E1EDL18 SEGMENT="1">
        <QUALF>CHG</QUALF>
      </E1EDL18>
      <E1EDL24 SEGMENT="1">
        <POSNR>000010</POSNR>
        <MATNR>61000</MATNR>
        <WERKS>CO01</WERKS>
        <LGORT>0001</LGORT>
        <CHARG>3PL_610051</CHARG>
        <LFIMG>1</LFIMG>
        <VRKME>EA</VRKME>
        <E1EDL11 SEGMENT="1">
          <SERNR>3PL_610051</SERNR>
        </E1EDL11>
        <E1EDL11 SEGMENT="1">
          <SERNR>3PL_610052</SERNR>
        </E1EDL11>
        <E1EDL11 SEGMENT="1">
          <SERNR>3PL_610053</SERNR>
        </E1EDL11>
        <E1EDL11 SEGMENT="1">
          <SERNR>3PL_610054</SERNR>
        </E1EDL11>
        <E1EDL11 SEGMENT="1">
          <SERNR>3PL_610055</SERNR>
        </E1EDL11>
      </E1EDL24>
      <E1EDL24 SEGMENT="1">
        <POSNR>000020</POSNR>
        <MATNR>61000</MATNR>
        <WERKS>CO01</WERKS>
        <LGORT>0001</LGORT>
        <CHARG>3PL_610052</CHARG>
        <LFIMG>1</LFIMG>
        <VRKME>EA</VRKME>
      </E1EDL24>
      <E1EDL24 SEGMENT="1">
        <POSNR>000030</POSNR>
        <MATNR>61000</MATNR>
        <WERKS>CO01</WERKS>
        <LGORT>0001</LGORT>
        <CHARG>3PL_610053</CHARG>
        <LFIMG>1</LFIMG>
        <VRKME>EA</VRKME>
      </E1EDL24>
      <E1EDL24 SEGMENT="1">
        <POSNR>000040</POSNR>
        <MATNR>61000</MATNR>
        <WERKS>CO01</WERKS>
        <LGORT>0001</LGORT>
        <CHARG>3PL_610054</CHARG>
        <LFIMG>1</LFIMG>
        <VRKME>EA</VRKME>
      </E1EDL24>
      <E1EDL24 SEGMENT="1">
        <POSNR>000050</POSNR>
        <MATNR>61000</MATNR>
        <WERKS>CO01</WERKS>
        <LGORT>0001</LGORT>
        <CHARG>3PL_610055</CHARG>
        <LFIMG>1</LFIMG>
        <VRKME>EA</VRKME>
      </E1EDL24>
    </E1EDL20>
  </IDOC>
</DELVRY03>

感谢您的协助和合作。如果您需要进一步的信息,或者我写的解释不够清楚,我会很专心。

0 个答案:

没有答案