我有一个客户端API,它以下列格式向我发送XML。我必须更改几个值并将相同的XML发送到我的客户端的另一个API。什么是最好的方法。我们目前正在使用SQL Server 2014和C#。
<?xml version="1.0"?>
<JobPositionPostings>
<JobPositionPosting>
<JobAction>ADD</JobAction>
<JobType>p</JobType>
<JobPositionPostingID>27</JobPositionPostingID>
<HiringOrg>
<HiringOrgName>Company Name</HiringOrgName>
<Industry>
<SummaryText>Brief description of the hiring organization</SummaryText>
</Industry>
<Contact>
<PersonName>
<FormattedName>Name of the Hiring Contact</FormattedName>
</PersonName>
</Contact>
</HiringOrg>
<JobDisplayOptions>
<MicrositeName>Client Microsite Name</MicrositeName>
<TemplateName>Client Template Name</TemplateName>
</JobDisplayOptions>
<JobPositionInformation>
<JobPositionTitle>R and D manager #2 banglore</JobPositionTitle>
<JobClassifications>
<PrimaryJobCategory>
<JobIndustryCode>7</JobIndustryCode>
<JobFunctionCode>8</JobFunctionCode>
<JobRoleCode>8.31</JobRoleCode>
</PrimaryJobCategory>
</JobClassifications>
<JobPositionDescription>
<JobKeywords>Java</JobKeywords>
<JobPositionLocation>
<InterNationalLoc></InterNationalLoc>
<InterNationalLocOther></InterNationalLocOther>
<IndianLoc>25.30.159</IndianLoc>
<IndianLocOther></IndianLocOther>
</JobPositionLocation>
<CompensationDescription>
<Pay>
<SalaryCurrency>Rupees</SalaryCurrency>
<MinimumSalary>250000</MinimumSalary>
<MaximumSalary>325000</MaximumSalary>
<DisplaySalary>Y</DisplaySalary>
</Pay>
<BenifitsDescription>Job Benefits Description</BenifitsDescription>
</CompensationDescription>
<SummaryText>Job Description(brief about JD)</SummaryText>
</JobPositionDescription>
<JobPositionRequirements>
<SummaryText>Desired Candidate Profile Summary Text - Required Competencies/Skills/Leadership E-mail
</SummaryText>
<JobExperience>
<MinimumExperience>2</MinimumExperience>
<MaximumExperience>4</MaximumExperience>
</JobExperience>
<JobQualifications>
<UGQualifications />
<UGSpecializations />
<PGQualifications />
<PGSpecializations />
</JobQualifications>
</JobPositionRequirements>
<AddJobsToRefSite>true</AddJobsToRefSite>
<ReferenceCode>Your Unique Job Reference code</ReferenceCode>
</JobPositionInformation>
<HowToApply>
<ApplicationMethods>
<ByEmail>
<E-mail>Client E-mail for Response Management , in case not using ATS Integration
</E-mail>
</ByEmail>
<ByWeb>
<URL></URL>
<ApplyIntegration>1</ApplyIntegration>
</ByWeb>
<QUESTIONNAIRE>Standard Questionnaire name if you know the same, else contact your Account manager</QUESTIONNAIRE>
</ApplicationMethods>
</HowToApply>
<ReferralDetails>
<Amount> 1000</Amount>
<Benefit> gift voucher</Benefit>
</ReferralDetails>
</JobPositionPosting>
<JobFeedResponseEmail>Tech person's E-mail for receiving Feed Status</JobFeedResponseEmail>
</JobPositionPostings>
我遵循以下方法。我用一些变量替换了我的XML中的所有值字段,用管道(|)符号分隔它们。请在下面找到替换的那个。
<?xml version="1.0"?>
<JobPositionPostings>
<JobPositionPosting>
<JobAction>|JAct_M|</JobAction>
<JobPositionPostingID>|JID_M|</JobPositionPostingID>
<JobType>|JType_M|</JobType>
<HiringOrg>
<WebSite>|JWebSite_N|</WebSite>
<HiringOrgName>|JComp_M|</HiringOrgName>
<Industry>
<SummaryText>|JCompDesc_M|</SummaryText>
</Industry>
<Contact>
<PersonName>
<FormattedName>|JContactName_N|</FormattedName>
</PersonName>
</Contact>
</HiringOrg>
<JobDisplayOptions>
<MicrositeName>|JMSite_S|</MicrositeName>
<TemplateName>|JTempl_N|</TemplateName>
</JobDisplayOptions>
<JobPositionInformation>
<JobPositionTitle>|JTitle_M|</JobPositionTitle>
<JobClassifications>
<PrimaryJobCategory>
<JobIndustryCode>|JIndustryCode_M|</JobIndustryCode>
<JobFunctionCode>|JFunCode_M|</JobFunctionCode>
<JobRoleCode>|JRoleCode_M|</JobRoleCode>
</PrimaryJobCategory>
</JobClassifications>
<JobPositionDescription>
<JobKeywords>|JSkills_M|</JobKeywords>
<JobPositionLocation>
<InterNationalLoc>|JIntlCode_M|</InterNationalLoc>
<InterNationalLocOther>|JIntlCodeOther_N|</InterNationalLocOther>
<IndianLoc>|JLocCode_M|</IndianLoc>
<IndianLocOther>|JLocOther_N|</IndianLocOther>
</JobPositionLocation>
<CompensationDescription>
<Pay>
<SalaryCurrency>|JCurr_M|</SalaryCurrency>
<MinimumSalary>|JMinSal_M|</MinimumSalary>
<MaximumSalary>|JMaxSal_M|</MaximumSalary>
<DisplaySalary>|JSalDisp_N|</DisplaySalary>
</Pay>
<BenifitsDescription>|JBenefits_N|</BenifitsDescription>
</CompensationDescription>
<SummaryText>|JJD_M|</SummaryText>
</JobPositionDescription>
<JobPositionRequirements>
<SummaryText>|JDesCand_N|</SummaryText>
<JobExperience>
<MinimumExperience>|JMinExp_M|</MinimumExperience>
<MaximumExperience>|JMaxExp_M|</MaximumExperience>
</JobExperience>
<JobQualifications>
<UGQualifications>|JQual1_M|</UGQualifications>
<UGSpecializations>|JSpec1_M|</UGSpecializations>
<PGQualifications>|JQual2_M|</PGQualifications>
<PGSpecializations>|JSpec2_M|</PGSpecializations>
<DoctorateQualifications>|JQual3_M|</DoctorateQualifications>
<DoctorateSpecializations>|JSpec3_M|</DoctorateSpecializations>
</JobQualifications>
</JobPositionRequirements>
<AddJobsToRefSite>|JRefSite_N|</AddJobsToRefSite>
<ReferenceCode>|JRefCode_N|</ReferenceCode>
</JobPositionInformation>
<HowToApply>
<ApplicationMethods>
<ByEmail>
<E-mail>|JLntMail_M|</E-mail>
</ByEmail>
<ByEapps>
<Eapps>|JeApps_M|</Eapps>
</ByEapps>
<ByWeb>
<URL>|JURL_M|</URL>
<ApplyIntegration>|JAppInt_M|</ApplyIntegration>
</ByWeb>
<QUESTIONNAIRE>|JQuest_N|</QUESTIONNAIRE>
</ApplicationMethods>
</HowToApply>
<ReferralDetails>
<Benefit>|JBenefit_N|</Benefit>
<Amount>|JBAmt_N|</Amount>
</ReferralDetails>
</JobPositionPosting>
<JobFeedResponseEmail>|JFeedRespEmail_N|</JobFeedResponseEmail>
</JobPositionPostings>
之后在SQL Server中我将它们替换为所需的值并将该XML转发给我的客户端。
set @Nxml = replace(@Nxml, '|JAct_M|','Add')
set @Nxml = replace(@Nxml, '|JID_M|',@reqmt_id)
set @Nxml = replace(@Nxml, '|JType_M|','P')
这是最好的方法还是有其他简单的方法。 提前谢谢。
答案 0 :(得分:0)
最好的方法(仅在您应该获取xml,修改它然后通过修改的xml传递)将使用XPathNavigator(更改JobAction的示例):
public static string UpdateJobAction(string inputXml)
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(inputXml);
var xpnavigator = xmlDocument.CreateNavigator();
foreach (XPathNavigator xpn in xpnavigator.Select("//JobPositionPostings/JobPositionPosting/JobAction"))
{
var value = xpn.Value; // to obtain the JobAction value
xpn.SetValue("Add");
}
return xpnavigator.OuterXml;
}
答案 1 :(得分:0)
在这种情况下,您正在修改许多元素。所以我建议使用序列化。以下是一个开始。我读取文件并反序列化,然后写出相同的结果。我建议您继续使用原始数据,并将输入和输出进行比较。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = @"c:\temp\test1.xml";
const string OUTPUT_FILENAME = @"c:\temp\test2.xml";
static void Main(string[] args)
{
JobPositionPostings postings = Load(INPUT_FILENAME);
// modify posting here
Save(OUTPUT_FILENAME, postings);
}
static JobPositionPostings Load(string filename)
{
StreamReader reader = new StreamReader(INPUT_FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(JobPositionPostings));
JobPositionPostings postings = (JobPositionPostings)serializer.Deserialize(reader);
return postings;
}
static void Save(string filename, JobPositionPostings postings)
{
XmlSerializer serializer = new XmlSerializer(typeof(JobPositionPostings));
StreamWriter writer = new StreamWriter(filename);
serializer.Serialize(writer, postings);
}
}
[XmlRoot("JobPositionPostings")]
public class JobPositionPostings
{
[XmlElement("JobPositionPosting")]
public List<JobPositionPosting> jobPositionPostings { get; set; }
}
[XmlRoot("JobPositionPosting")]
public class JobPositionPosting
{
[XmlElement("JobAction")]
public string JobAction { get; set; }
[XmlElement("JobType")]
public string jobType { get; set; }
[XmlElement("JobPositionPostingID")]
public int jobPositionPostingID { get; set; }
[XmlElement("HiringOrg")]
public HiringOrg hiringOrg { get; set; }
[XmlElement("JobDisplayOptions")]
public JobDisplayOptions jobDisplayOptions { get; set; }
}
[XmlRoot("HiringOrg")]
public class HiringOrg
{
[XmlElement("HiringOrgName")]
public string hiringOrgName { get; set; }
[XmlElement("Industry")]
public Industry industry { get; set; }
[XmlElement("Contact")]
public Contact contact { get; set; }
}
[XmlRoot("Industry")]
public class Industry
{
[XmlElement("SummaryText")]
public string summaryText { get; set; }
[XmlElement("Contact")]
public string contact { get; set; }
}
[XmlRoot("Contact")]
public class Contact
{
[XmlElement("PersonName")]
public PersonName personName { get; set; }
}
[XmlRoot("PersonName")]
public class PersonName
{
[XmlElement("FormattedName")]
public string formattedName { get; set; }
}
[XmlRoot("JobDisplayOptions")]
public class JobDisplayOptions
{
[XmlElement("MicrositeName")]
public string micrositeName { get; set; }
[XmlElement("TemplateName")]
public string templateName { get; set; }
}
}
答案 2 :(得分:0)
要使用SQL-Server修改XML,您可以使用XML数据类型的函数.modify()
。首先,您必须将XML转换/转换为真实 XML类型。
不要在字符串级别处理XML!
检查一下(为简洁起见缩短):
DECLARE @xml XML=
N'<?xml version="1.0"?>
<JobPositionPostings>
<JobPositionPosting>
<JobAction>ADD</JobAction>
<JobType>p</JobType>
<JobPositionPostingID>27</JobPositionPostingID>
<HiringOrg>
<HiringOrgName>Company Name</HiringOrgName>
<Industry>
<SummaryText>Brief description of the hiring organization</SummaryText>
</Industry>
<Contact>
<PersonName>
<FormattedName>Name of the Hiring Contact</FormattedName>
</PersonName>
</Contact>
</HiringOrg>
<JobDisplayOptions>
<MicrositeName>Client Microsite Name</MicrositeName>
<TemplateName>Client Template Name</TemplateName>
</JobDisplayOptions>
<!-- More nodes -->
</JobPositionPosting>
<JobFeedResponseEmail>Tech person''s E-mail for receiving Feed Status</JobFeedResponseEmail>
</JobPositionPostings>';
- 使用硬编码新值替换一个特定现有元素的值
SET @xml.modify(N'replace value of (/JobPositionPostings/JobPositionPosting/JobAction/text())[1] with "SomeNewValue"');
- 您可以使用sql:variable("@SomeVariable")
或sql:column("SomeColumn")
动态获取值
DECLARE @NewJobType NVARCHAR(100)='NewType';
SET @xml.modify(N'replace value of (/JobPositionPostings/JobPositionPosting/JobType/text())[1] with sql:variable("@NewJobType")');
- 在特定位置引入新元素
SET @xml.modify(N'insert <SomeNewElement>test</SomeNewElement> after (/JobPositionPostings/JobPositionPosting/JobType)[1]');
- 删除一个(完整)元素
SET @xml.modify(N'delete (/JobPositionPostings/JobPositionPosting/HiringOrg)[1]');
- 检查修改后的结果
SELECT @xml;
<JobPositionPostings>
<JobPositionPosting>
<JobAction>SomeNewValue</JobAction>
<JobType>NewType</JobType>
<SomeNewElement>test</SomeNewElement>
<JobPositionPostingID>27</JobPositionPostingID>
<JobDisplayOptions>
<MicrositeName>Client Microsite Name</MicrositeName>
<TemplateName>Client Template Name</TemplateName>
</JobDisplayOptions>
<!-- More nodes -->
</JobPositionPosting>
<JobFeedResponseEmail>Tech person's E-mail for receiving Feed Status</JobFeedResponseEmail>
</JobPositionPostings>