0

XML ファイルの読み取りに関する問題を修正しました。私が今必要としているのは、日時を MM dd yyyy hh:mm:ss だけにトリミングし、Informix データベースに挿入するときに残りを持ち込まないようにすることです。

これは XML 情報です。

 <RecordFilingRequestMessage xmlns:nc="http://niem.gov/niem/niem-core/2.0">
<nc:DocumentIdentification>
  <nc:IdentificationID>3212842</nc:IdentificationID>
</nc:DocumentIdentification>
<nc:DocumentPostDate>
  <nc:DateTime>2013-06-25T11:32:08.5343733-04:00</nc:DateTime>
</nc:DocumentPostDate>
<nc:DocumentSubmitter>
  <ecf:EntityPerson s:id="REVIEWER">
    <nc:PersonName />
    <nc:PersonOtherIdentification>
      <nc:IdentificationID>41130</nc:IdentificationID>
      <nc:IdentificationCategoryText>FLEPORTAL</nc:IdentificationCategoryText>
    </nc:PersonOtherIdentification>
    <nc:PersonOtherIdentification>
      <nc:IdentificationID>kacolburn</nc:IdentificationID>
      <nc:IdentificationCategoryText>FLEPORTAL_LOGONNAME</nc:IdentificationCategoryText>
    </nc:PersonOtherIdentification>

...そしてここに私のC#コードがあります:

string DocID = null;
        int elementCount = 0;
        string reqID = null;
        string reqDateTime = null;
        string empName = null;
        string[] fileEntries = Directory.GetFiles(@"C:\XML\3212842.xml");
        foreach (string fileName in fileEntries)
        {
            XmlReader xr = XmlReader.Create(fileName); //reads XML from folder
            while (xr.Read())
            {
                if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:DateTime")
                {
                    reqDateTime = xr.ReadElementContentAsString();                   
                }
                if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:IdentificationID")
                {
                    elementCount++;
                    DocID = xr.ReadElementContentAsString();
                    if (elementCount == 1)
                    {
                        reqID = DocID;
                    }
                    if (elementCount == 3)
                    {
                        empName = DocID;
                        listBox1.Items.Add(reqID + " / " + reqDateTime + " / " + empName);
                        elementCount = 0;
                        break;
                    }
4

2 に答える 2

0

私の最初の考えは、最後の「/」は「SelectNodes」呼び出しに属していないということです。

または、次のコードで問題を解決できます。

foreach(XmlNode node in xmlDoc.GetElementsByTagName("RecordFilingRequest")[0].GetElementsByTagName("nc:DocumentIdentification"))
{
    int ID = Convert.ToInt32(node.FirstChild().InnerText);
}

編集:これは、「RecordFilingRequest」が常に存在することを前提としています。そうでない場合は、try .. catch ステートメントを追加します。

于 2013-06-25T20:28:48.060 に答える