0

I have fixed my issue with reading the XML files. What I am needing now is to trim the datetime down to just MM dd yyyy hh:mm:ss and not bring the rest of it over when I insert into my Informix database.

This is the XML info:

 <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>

...and here is my C# code:

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;
                    }
  • `SelectNodes` takes an XPath query. There are two possible problems here: you're not including the XML namespaces for the elements, and you're also not passing what looks like a valid XPath. – GalacticCowboy Jun 25 '13 at 20:28

2 Answers2

0

Looks like the issue is that the XML uses namespaces, and your XPath does not. You didn't post your full XML, but you probably have something like xmlns:nc="http://some.url/ in there. Make sure to include a the namespace in a namespace manager, then add namespace prefixes to your query:

var nameTable = new NameTable();
var nsMgr = new XmlNamespaceManager(nameTable);
nsmgr.AddNamespace("nc", "http://some.url/");

var dataNodes = xmlDoc.SelectNodes("nc:RecordFilingRequest/nc:DocumentIdentification", nsMgr);
foreach (var node in dataNodes)
{
    var ID = Convert.ToInt32(node.SelectSingleNode("nc:IdentificationID", nsMgr).InnerText);
    // insert into database, e.g. using SqlCommand or whatever
}
Jacob
  • 77,566
  • 24
  • 149
  • 228
0

My first thought is, that the last '/' doesn't belong to the 'SelectNodes' call.

Alternatively, this code will solve your problem:

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

Edit: This does assume, that 'RecordFilingRequest' always exists. Add a try .. catch statement, if that isn't the case.

Jan Berktold
  • 976
  • 1
  • 11
  • 33
  • The second "GetElementsByTagName" was not liked in VS 2010. Why would this be? – Raymond Beyrle Jun 25 '13 at 21:01
  • Am I missing something simple here? FirstChild or SelectSingleNode is not being accepted and I am still hitting the error on the second GetElementsByTagName. I appreciate your assistance so far. XML is so complicated – Raymond Beyrle Jun 27 '13 at 15:10