日期:2014-05-18  浏览次数:20847 次

关于写XML的问题
有个问题请教下,现在有这么个XML
XML code

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<NewCont>
  <BaseInfo>
    <Missionid>00000000000058824904</Missionid>
    <SubMissionid>1</SubMissionid>
    <ActivityId>0000001002</ActivityId>
    <Managecom>86440601</Managecom>
    <PrtSeq>8104401614213</PrtSeq>
    <ContNo>00000035725392</ContNo>
  </BaseInfo>
  <ContInfo>
    <LCCont>
      <ContNo>00000035725392</ContNo>
      <PolApplyDate>2012-06-22</PolApplyDate>
      <CvaliDate>2012-06-22</CvaliDate>
      <AgentCode>37054128</AgentCode>
      <AgentName>黄东生</AgentName>
    </LCCont>
  </ContInfo>
</NewCont>



客户要的格式是这样的,但是我现在从数据库中读出来的是一整串的,如:
"Missionid=00000000000058824904;SubMissionid=1;ActivityId=0000001002;AgentCode=37054128;CvaliDate=2012-06-22;ContNo=00000035725392;AgentCode=37054128;",我拿到的顺序可能是乱掉的,也可能有些节点没有,请问,我该如何按照客户的格式(包括顺序也要一样)写成一个XML呢?

------解决方案--------------------
1、将数据库的字段通过split(';').split('=')后然后定义先后顺序,Index
2、通过检索顺序进行xmlnode赋值
------解决方案--------------------
C# code
 protected void Page_Load(object sender, EventArgs e)
    {
        var str = "Missionid=00000000000058824904;SubMissionid=1;ActivityId=0000001002;AgentCode=37054128;CvaliDate=2012-06-22;ContNo=00000035725392";

        var xml0 = @"<BaseInfo>
<Missionid>{0}</Missionid>
<SubMissionid>{1}</SubMissionid>
<ActivityId>{2}</ActivityId>
<Managecom>{3}</Managecom>
<PrtSeq>{4}</PrtSeq>
<ContNo>{5}</ContNo>
</BaseInfo>";
        var xml = string.Format(xml0, GetValue(str,"Missionid"), GetValue(str,"SubMissionid"), GetValue(str,"ActivityId"), GetValue(str,"Managecom"), GetValue(str,"PrtSeq"), GetValue(str,"ContNo"));

        TextBox1.Text = xml;
    }
    string GetValue(string inStr,string filedName)
    {
        var reg = filedName + "=([^;]+)";
        var m = Regex.Match(inStr, reg);
        return m.Success ? m.Groups[1].Value : "";
    }