← Back to team overview

dhis2-users team mailing list archive

Need assistance on web api dataValueSets

 

Hi,

I have written some code for an application that I am developing for the
organisation enterprise system to post program data to DHIS2 as the
backend. Am not sure why the data posted is not being parsed to the DHIS2
database when I click on submit button, yet the created XML (from the
'makeXML' method) file can be uploaded directly into DHIS2 using the
import/export module of DHIS2.

Kindly assist.

Thanks.


/*This is the code that is supposed to send the data to DHIS2*/

protected void btnsubmit_Click(object sender, EventArgs e)
        {
            makeXML();
            byte[] bytes;
            bytes =
System.Text.Encoding.ASCII.GetBytes(@"c:\users\app-dev\desktop\test.xml");
            string uname = "admin", pwd = "district";
            string formattedUrl = "http://localhost/api/dataValueSets";;
            string credentials =
Convert.ToBase64String(Encoding.ASCII.GetBytes(uname + ":" + pwd));
            HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(formattedUrl);
            request.Headers["Authorization"] = "Basic " + credentials;
            request.ContentType = "application/xml";
            request.Method = "POST";
            request.PreAuthenticate = true;
            request.ContentLength = bytes.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();

        }

/*This will create an XML file from the data entry form of the site using
DHIS2 dxf format*/

        private void makeXML()
        {
            string returnClient = "XLSwl4Kp8G9", newClient = "sKc1MwYPg4k",
mlut = "X5d6f0KqljI", attrib = "MdydnY0BTLI";
            XmlWriterSettings set = new XmlWriterSettings();
            set.Indent = true;
            set.IndentChars = "  ";
            set.NewLineOnAttributes = false;
            set.NewLineHandling = NewLineHandling.None;
            using (XmlWriter wr =
XmlWriter.Create(@"c:\users\app-dev\desktop\test.xml", set))
            {
                wr.WriteStartDocument();
                wr.WriteStartElement("dataValueSet", "
http://dhis2.org/schema/dxf/2.0";);
                wr.WriteStartElement("dataValue");
                wr.WriteAttributeString("dataElement", mlut);
                wr.WriteAttributeString("period", "201503");
                wr.WriteAttributeString("orgUnit", "GFL5QBPE4sw");
                wr.WriteAttributeString("categoryOptionCombo", newClient);
                wr.WriteAttributeString("attributeOptionCombo", attrib);
                wr.WriteAttributeString("value",
txtmicrolutN.Text.ToString());
                wr.WriteEndElement();
                wr.WriteStartElement("dataValue");
                wr.WriteAttributeString("dataElement", mlut);
                wr.WriteAttributeString("period", "201503");
                wr.WriteAttributeString("orgUnit", "GFL5QBPE4sw");
                wr.WriteAttributeString("categoryOptionCombo",
returnClient);
                wr.WriteAttributeString("attributeOptionCombo", attrib);
                wr.WriteAttributeString("value",
txtmicrolutR.Text.ToString());
                wr.WriteEndElement();
                wr.WriteEndDocument();
            }
        }

//Below is sample XML file created from the above method

<?xml version="1.0" encoding="utf-8"?>
<dataValueSet xmlns="http://dhis2.org/schema/dxf/2.0";>
  <dataValue dataElement="X5d6f0KqljI" period="201503"
orgUnit="GFL5QBPE4sw" categoryOptionCombo="sKc1MwYPg4k"
attributeOptionCombo="MdydnY0BTLI" value="13" />
  <dataValue dataElement="X5d6f0KqljI" period="201503"
orgUnit="GFL5QBPE4sw" categoryOptionCombo="XLSwl4Kp8G9"
attributeOptionCombo="MdydnY0BTLI" value="103" />
</dataValueSet>