创建以下XML的信封

时间:2018-11-21 22:11:32

标签: c# .net xml linq-to-xml xmldocument

我在这种情况下挣扎了一段时间!!我真的希望我能从您那里得到一些帮助。

在一种方法中,我以字符串形式接收此XML:

<?xml version="1.0" encoding="utf-16" standalone="no"?>
<VC_001_CreateDocument>
  <VCRequest>
    <Header>
      <OrganisationData>
        <ClientId />
        <UserId />
        <Pass />
      </OrganisationData>
      <Article>
        <OutcomeSource>ERP</OutcomeSource>
        <ArticleNumber>6034967-Sample</ArticleNumber>
        <ProductNumbers>
          <ProductNumber Type="GTIN" Level="PRI" />
        </ProductNumbers>
        <Forecast />
        <ERPStatus>APP</ERPStatus>
        <SerialisationFlag />
        <CSDBArticleNumber>6034967-Sample</CSDBArticleNumber>
        <ArticleDescription>Rose</ArticleDescription>
        <WorkflowId />
        <CommonName />
        <PharmaceuticalForm />
        <Strength />
        <PackageWeight />
        <PackageSize />
        <PackageType />
        <GS1GLN />
        <GS1CompanyPrefix />
        <Customer>
          <CustomerId />
          <CustomerErpNumber />
        </Customer>
        <ShelfLife />
        <Region />
        <ProductionSites>
          <ProductionSite>
            <ProductionSiteId />
            <ProductionSiteErpNumber />
          </ProductionSite>
        </ProductionSites>
        <GenericArticleField01 />
        <GenericArticleField02 />
        <GenericArticleField03 />
        <GenericArticleField04 />
        <GenericArticleField05 />
        <RequiredFields>
          <RequiredField Name="" />
        </RequiredFields>
        <Comment />
        <Checked />
        <TargetMarkets>
          <TargetMarket>
            <GS1NHRN />
            <GenericFields Type="" Language="" />
            <Mah />
            <Wholesaler />
          </TargetMarket>
        </TargetMarkets>
        <VerificationSystem />
        <Email />
        <FixData />
        <StartValueInitial />
        <SubPools>
          <SubPool>
            <AggregationLevel />
            <PoolIdentProductNumber />
            <QuantityPerLevel />
            <IncompletePackagingRule />
            <QuantityOfLayer />
            <GenerationPattern />
            <PostProductionSerialNumberAssignment />
            <PrePrinting />
            <Factor />
            <Threshold />
            <SerialNumberType />
            <ExtensionDigit />
            <SerialNumberSource />
            <DeliveranceInformation>
              <MinimumValue />
              <PercentualAmount />
            </DeliveranceInformation>
          </SubPool>
        </SubPools>
        <Report>
          <ReportExternal />
          <ReportVerificationSystem />
        </Report>
      </Article>
    </Header>
  </VCRequest>
</VC_001_CreateDocument>

完全像这样在信封中转换以在我的SOAP请求中使用:如果有的话,读取节点值!

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:csdb="http://site.de/csdb">
  <soapenv:Header/>
  <soapenv:Body>
    <csdb:VC_001_CreateDocument>
      <csdb:VARequest>
        <csdb:Header>
          <csdb:OrganisationData>
            <csdb:ClientId>EDP</csdb:ClientId>
            <csdb:UserId>webservice</csdb:UserId>
            <csdb:Pass>!password*</csdb:Pass>
          </csdb:OrganisationData>
        </csdb:Header>
        <csdb:Article>
          <csdb:OutcomeSource>ERP</csdb:OutcomeSource>
          <csdb:ArticleNumber>6034967-Sample</csdb:ArticleNumber>
          <csdb:ProductNumbers>
            <csdb:ProductNumber type="GTIN" level="PRI"></csdb:ProductNumber>
          </csdb:ProductNumbers>
          <csdb:Forecast></csdb:Forecast>
          <csdb:ERPStatus></csdb:ERPStatus>
          <csdb:SerialisationFlag></csdb:SerialisationFlag>
          <csdb:CSDBArticleNumber>6034967-AMOSTRA</csdb:CSDBArticleNumber>
          <csdb:ArticleDescription>PINOX ROSA</csdb:ArticleDescription>
          <csdb:WorkflowId></csdb:WorkflowId>
          <csdb:CommonName></csdb:CommonName>
          <csdb:PharmaceuticalForm></csdb:PharmaceuticalForm>
          <csdb:Strength></csdb:Strength>
          <csdb:PackageWeight></csdb:PackageWeight>
          <csdb:PackageSize></csdb:PackageSize>
          <csdb:PackageType></csdb:PackageType>
          <csdb:GS1GLN></csdb:GS1GLN>
          <csdb:GS1CompanyPrefix></csdb:GS1CompanyPrefix>
          <csdb:Customer>
            <csdb:Customer></csdb:Customer>
            <csdb:CustomerId></csdb:CustomerId>
            <csdb:CustomerErpNumber></csdb:CustomerErpNumber>
          </csdb:Customer>>
          <csdb:ShelfLife></csdb:ShelfLife>
          <csdb:Region></csdb:Region>
          <csdb:ProductionSites>
            <csdb:ProductionSite>
              <csdb:ProductionSiteId></csdb:ProductionSiteId>
              <csdb:ProductionSiteErpNumber></csdb:ProductionSiteErpNumber>
            </csdb:ProductionSite>
          </csdb:ProductionSites>
          <csdb:GenericArticleField01></csdb:GenericArticleField01>
          <csdb:GenericArticleField02></csdb:GenericArticleField02>
          <csdb:GenericArticleField03></csdb:GenericArticleField03>
          <csdb:GenericArticleField04></csdb:GenericArticleField04>
          <csdb:GenericArticleField05></csdb:GenericArticleField05>
          <csdb:RequiredFields>
            <csdb:RequiredField name="?"></csdb:RequiredField>
          </csdb:RequiredFields>
          <csdb:Comment></csdb:Comment>
          <csdb:Checked></csdb:Checked>
          <csdb:GS1NHRN GS1NHRNNational="?"></csdb:GS1NHRN>
          <csdb:TargetMarkets>
            <csdb:TargetMarket targetMarket="?">
              <csdb:GS1NHRN GS1NHRNNational="?"></csdb:GS1NHRN>
              <csdb:GenericFields type="?" language="?"></csdb:GenericFields>
              <csdb:Mah></csdb:Mah>
              <csdb:Wholesaler></csdb:Wholesaler>
            </csdb:TargetMarket>
          </csdb:TargetMarkets>
          <csdb:Verificationsystem></csdb:Verificationsystem>
          <csdb:Email></csdb:Email>
          <csdb:FixData></csdb:FixData>
          <csdb:StartValueInitial></csdb:StartValueInitial>
          <csdb:Subpools>
            <csdb:Subpool>
              <csdb:AggregationLevel></csdb:AggregationLevel>
              <csdb:PoolIdentProductNumber type="?"></csdb:PoolIdentProductNumber>
              <csdb:QuantityPerLevel></csdb:QuantityPerLevel>
              <csdb:IncompletePackagingRule></csdb:IncompletePackagingRule>
              <csdb:QuantityOfLayer></csdb:QuantityOfLayer>
              <csdb:GenerationPattern></csdb:GenerationPattern>
              <csdb:PostProductionSerialNumberAssignment></csdb:PostProductionSerialNumberAssignment>
              <csdb:PrePrinting></csdb:PrePrinting>
              <csdb:Factor></csdb:Factor>
              <csdb:Threshold></csdb:Threshold>
              <csdb:SerialNumberType></csdb:SerialNumberType>
              <csdb:ExtensionDigit></csdb:ExtensionDigit>
              <csdb:SerialNumberSource></csdb:SerialNumberSource>
              <csdb:DeliveranceInformation>
                <csdb:MinimumValue></csdb:MinimumValue>
                <csdb:PercentualAmount></csdb:PercentualAmount>
              </csdb:DeliveranceInformation>
            </csdb:Subpool>
          </csdb:Subpools>
          <csdb:Report>
            <csdb:ReportExternal></csdb:ReportExternal>
            <csdb:ReportVerificationSystem></csdb:ReportVerificationSystem>
          </csdb:Report>
        </csdb:Article>
      </csdb:VCRequest>
    </csdb:VC_001_CreateDocument>
  </soapenv:Body>
</soapenv:Envelope>

这正是我收到的XML,这是我必须创建的信封(由SOAPUI生成的请求),节点当然具有动态值,这些参数仅是示例。 我认为这是最简单的方法,因为我无法“添加服务引用”,因为我正在尝试开发将嵌套Web服务的DLL。没有将要安装此DLL的web.config。 非常感谢您帮助我解决导致我发疯的问题

1 个答案:

答案 0 :(得分:0)

使用xml linq:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine(); // skip the utf-16 in header that Net Library doesn't accept
            XDocument doc = XDocument.Load(reader);

            string soapHeader = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:csdb=\"http://site.de/csdb\">" +
                "<soapenv:Header/>" +
                "<soapenv:Body>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>";

            XElement soap = XElement.Parse(soapHeader);
            XNamespace nsCsdb = soap.GetNamespaceOfPrefix("csdb");
            XNamespace nsSoapenv = soap.GetNamespaceOfPrefix("soapenv");
            XElement body = soap.Descendants(nsSoapenv + "Body").FirstOrDefault();

            body.Add(doc.Root);
            foreach (XElement child in body.Descendants())
            {
                child.Name = nsCsdb.GetName(child.Name.LocalName);
                List<XAttribute> atList = child.Attributes().ToList();
                child.Attributes().Remove();
                foreach (XAttribute at in atList)
                    child.Add(new XAttribute(nsCsdb.GetName(at.Name.LocalName), at.Value));
            }
        }
    }
}