从SOAP消息中删除名称空间

本文关键字:空间 删除 SOAP 消息 | 更新日期: 2023-09-27 18:12:29

我有一个问题……

这是一个我必须传递给某个方法的XML文档。

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header/>
<SOAP:Body>
<BATMAS03>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>310</MANDT>
<DOCNUM>0000000000006284</DOCNUM>
<DOCREL>740</DOCREL>
<STATUS>30</STATUS>
<DIRECT>1</DIRECT>
<OUTMOD>2</OUTMOD>
<IDOCTYP>BATMAS03</IDOCTYP>
<MESTYP>BATMAS</MESTYP>
<STDMES>BATMAS</STDMES>
<SNDPOR>SAPDVJ</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>DVJCLNT310</SNDPRN>
<RCVPOR>XML</RCVPOR>
<RCVPRT>LS</RCVPRT>
<RCVPRN>DXTCLNT330</RCVPRN>
<CREDAT>20150902</CREDAT>
<CRETIM>193043</CRETIM>
</EDI_DC40>
<E1BATMAS SEGMENT="1">
<MATERIAL>000000000000501104</MATERIAL>
<BATCH>150715L01</BATCH>
<E1BPBATCHCTRL SEGMENT="1">
<DOCLASSIFY>X</DOCLASSIFY>
</E1BPBATCHCTRL>
<E1BP3060_ALLOCATION SEGMENT="1">
<CLASS_TYPE>023</CLASS_TYPE>
<OBJECTKEY>000000000000501104150715L01</OBJECTKEY>
<OBJECTTABLE>MCH1</OBJECTTABLE>
<CLOBJECTKEY>Z_PROIZVOD</CLOBJECTKEY>
<CLASSNUM>Z_PROIZVOD</CLASSNUM>
<CLASSTYPE>023</CLASSTYPE>
</E1BP3060_ALLOCATION>
<E1BP3060_VALUATION_CHAR SEGMENT="1">
<CLASS_TYPE>023</CLASS_TYPE>
<OBJECTKEY>000000000000501104150715L01</OBJECTKEY>
<OBJECTTABLE>MCH1</OBJECTTABLE>
<CLOBJECTKEY>Z_PROIZVOD</CLOBJECTKEY>
<CHARACT>Z_EDICIJA</CHARACT>
<VALUE_CHAR>V001</VALUE_CHAR>
</E1BP3060_VALUATION_CHAR>
</E1BATMAS>
</IDOC>
</BATMAS03>
</SOAP:Body>
</SOAP:Envelope>

首先我把它转换成这样的字符串

 StringWriter sw = new StringWriter();
 XmlTextWriter xw = new XmlTextWriter(sw);
 XmlDoc.WriteTo(xw);
 string str = sw.ToString();

然后我把那个字符串传递给一个方法。问题是我想要删除或传递没有SOAP声明的字符串,没有这个;

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header/>
<SOAP:Body>

这是我想传递的字符串:

<BATMAS03>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>310</MANDT>
<DOCNUM>0000000000006284</DOCNUM>
<DOCREL>740</DOCREL>
<STATUS>30</STATUS>
<DIRECT>1</DIRECT>
<OUTMOD>2</OUTMOD>
<IDOCTYP>BATMAS03</IDOCTYP>
<MESTYP>BATMAS</MESTYP>
<STDMES>BATMAS</STDMES>
<SNDPOR>SAPDVJ</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>DVJCLNT310</SNDPRN>
<RCVPOR>XML</RCVPOR>
<RCVPRT>LS</RCVPRT>
<RCVPRN>DXTCLNT330</RCVPRN>
<CREDAT>20150902</CREDAT>
<CRETIM>193043</CRETIM>
</EDI_DC40>
<E1BATMAS SEGMENT="1">
<MATERIAL>000000000000501104</MATERIAL>
<BATCH>150715L01</BATCH>
<E1BPBATCHCTRL SEGMENT="1">
<DOCLASSIFY>X</DOCLASSIFY>
</E1BPBATCHCTRL>
<E1BP3060_ALLOCATION SEGMENT="1">
<CLASS_TYPE>023</CLASS_TYPE>
<OBJECTKEY>000000000000501104150715L01</OBJECTKEY>
<OBJECTTABLE>MCH1</OBJECTTABLE>
<CLOBJECTKEY>Z_PROIZVOD</CLOBJECTKEY>
<CLASSNUM>Z_PROIZVOD</CLASSNUM>
<CLASSTYPE>023</CLASSTYPE>
</E1BP3060_ALLOCATION>
<E1BP3060_VALUATION_CHAR SEGMENT="1">
<CLASS_TYPE>023</CLASS_TYPE>
<OBJECTKEY>000000000000501104150715L01</OBJECTKEY>
<OBJECTTABLE>MCH1</OBJECTTABLE>
<CLOBJECTKEY>Z_PROIZVOD</CLOBJECTKEY>
<CHARACT>Z_EDICIJA</CHARACT>
<VALUE_CHAR>V001</VALUE_CHAR>
</E1BP3060_VALUATION_CHAR>
</E1BATMAS>
</IDOC>
</BATMAS03>

这是可能的吗?谢谢!

从SOAP消息中删除名称空间

这将创建没有名称空间的myObject。

XmlDocument myObject = new XmlDocument();
using (XmlTextReader tr = new XmlTextReader(xmlFilepath))
{
    tr.Namespaces = false;
    myObject.Load(tr);
}

我也尝试过创建一个对象并处理删除一些不需要的节点,然后将其写入另一个文件。

对不起,我没有读对,我以为你只对删除命名空间感兴趣。这是您需要对前面的代码

进行的更新
//this assumes your SOAP:Body is a well known tag
//else you may wish to make it a bit generic
XmlNodeList nodeList = myObject.GetElementsByTagName("SOAP:Body");
if (nodeList.Item(0)!= null)
{
    XmlNode item = nodeList.Item(0);
    if (item != null)
    File.WriteAllText("TestOutput.xml", item.InnerXml);
}