gpt4 book ai didi

c# - 如何在 c#.net 中更新 soap xml 文件元素值

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:40 25 4
gpt4 key购买 nike

如何使用 c#.net 从 soap xml 文件更新 SOAP xml 元素值?我的文件如下:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<Action xmlns="http://www.w3.org/2005/08/addressing">http://extn.ssac.ee.sim.dsh.cms.hhs.gov/SsaCompositePortType/VerifySSAResponse</Action>
<MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:27f0af81-0da7-441f-83a7-fb2beaf30664</MessageID>
<RelatesTo xmlns="http://www.w3.org/2005/08/addressing">uuid:e8dcd685-cb86-400a-8059-dcad985a7ea7</RelatesTo>
</soap:Header>
<soap:Body>
<exch:SSACompositeResponse xmlns:exch="http://ssac.ee.sim.dsh.cms.hhs.gov">
<ssac:SSACompositeIndividualResponse xmlns:ssac="http://extn.ssac.ee.sim.dsh.cms.hhs.gov">
<ssac:ResponseMetadata>
<ssac:ResponseCode>HS000000</ssac:ResponseCode>
<ssac:ResponseDescriptionText>Success</ssac:ResponseDescriptionText>
</ssac:ResponseMetadata>
<ssac:PersonSSNIdentification>199123051</ssac:PersonSSNIdentification>
<ssac:SSAResponse>
<ssac:SSNVerificationIndicator>true</ssac:SSNVerificationIndicator>
<ssac:DeathConfirmationCode>Unconfirmed</ssac:DeathConfirmationCode>
<nc:PersonUSCitizenIndicator xmlns:nc="http://niem.gov/niem/niem-core/2.0">true</nc:PersonUSCitizenIndicator>
<ssac:PersonIncarcerationInformationIndicator>false</ssac:PersonIncarcerationInformationIndicator>
<ssac:SSATitleIIAnnualIncomeInformationIndicator>false</ssac:SSATitleIIAnnualIncomeInformationIndicator>
<ssac:SSATitleIIMonthlyIncomeInformationIndicator>false</ssac:SSATitleIIMonthlyIncomeInformationIndicator>
<ssac:SSAQuartersOfCoverageInformationIndicator>false</ssac:SSAQuartersOfCoverageInformationIndicator>
</ssac:SSAResponse>
</ssac:SSACompositeIndividualResponse>
</exch:SSACompositeResponse>

这里我想更新 ssac:PersonSSNIdentification (199123051) 值到 9876543210?

                XmlNamespaceManager xnm;
xnm = new XmlNamespaceManager(new NameTable());
xnm.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
var xmlDoc = new XmlDocument();
xmlDoc.Load(newSSAPathName);
XmlNodeList aNodes = xmlDoc.SelectNodes("/Envelope/Body/SSACompositeResponse/SSACompositeIndividualResponse");
foreach (XmlNode aNode in aNodes)
{ var innerXmlNode = aNode.SelectSingleNode("/PersonSSNIdentification");
if (innerXmlNode != null)
{ innerXmlNode.Value = lstSsn[0];
}
}
xmlDoc.Save(newSSAPathName);

但这行不通...有什么办法...

最佳答案

我建议使用 LINQ to XML 执行此操作而不是旧的 XmlDocument API。

首先,将您的 XML 加载到 XDocument 中:

        var doc = XDocument.Load(newSSAPathName);

然后,定义一个XNamespace对于您的 XML 中的每个命名空间:

        var soap = (XNamespace)"http://www.w3.org/2003/05/soap-envelope";
var exch = (XNamespace)"http://ssac.ee.sim.dsh.cms.hhs.gov";
var ssac = (XNamespace)"http://extn.ssac.ee.sim.dsh.cms.hhs.gov";

现在查询所有elements在具有所需完全限定名称的 XML 层次结构中的所需深度:

        var ssnIds = doc.Elements(soap + "Envelope")
.Elements(soap + "Body")
.Elements(exch + "SSACompositeResponse")
.Elements(ssac + "SSACompositeIndividualResponse")
.Elements(ssac + "PersonSSNIdentification");

最后,更改适当的元素值:

        foreach (var ssnId in ssnIds)
if (ssnId.Value == "199123051")
ssnId.Value = "9876543210";

或者,如果您确实想使用 XPATH 查询,则需要填充 XmlNamespaceManager具有所有 3 个 namespace 。 documentation对于 XPathSelectElements(XNode, String, IXmlNamespaceResolver) 展示了如何执行此操作。然后使用您选择的与 XPATH 查询中的每个命名空间相对应的前缀:

        using (var reader = XmlReader.Create(newSSAPathName))
{
var doc = XDocument.Load(reader);

// Set up a namespaceManager as described in https://msdn.microsoft.com/en-us/library/bb351355%28v=vs.110%29.aspx
var nameTable = reader.NameTable;
var namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
namespaceManager.AddNamespace("exch", "http://ssac.ee.sim.dsh.cms.hhs.gov");
namespaceManager.AddNamespace("ssac", "http://extn.ssac.ee.sim.dsh.cms.hhs.gov");

var ssnIds = doc.XPathSelectElements("soap:Envelope/soap:Body/exch:SSACompositeResponse/ssac:SSACompositeIndividualResponse/ssac:PersonSSNIdentification", namespaceManager);

foreach (var ssnId in ssnIds)
if (ssnId.Value == "199123051")
ssnId.Value = "9876543210";
}

请务必在文件开头执行 using System.Xml.XPath;,因为 XPathSelectElements 是一种扩展方法。

更新

使用 XmlDocument,等效的 XPATH 查询是:

        var xmlDoc = new XmlDocument();
xmlDoc.Load(newSSAPathName);

var nameTable = xmlDoc.NameTable;
var namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
namespaceManager.AddNamespace("exch", "http://ssac.ee.sim.dsh.cms.hhs.gov");
namespaceManager.AddNamespace("ssac", "http://extn.ssac.ee.sim.dsh.cms.hhs.gov");

var ssnIds = xmlDoc.SelectNodes("soap:Envelope/soap:Body/exch:SSACompositeResponse/ssac:SSACompositeIndividualResponse/ssac:PersonSSNIdentification", namespaceManager);

foreach (XmlNode ssnId in ssnIds)
if (ssnId.InnerText == "199123051")
ssnId.InnerText = "9876543210";

关于c# - 如何在 c#.net 中更新 soap xml 文件元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34613679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com