Well, I am trying to append data using C programming and libxml2 modulel but am facing a lot of problems as I am fairly new to this. 
My code is designed to first fetch me an Element Node from the XML file based on the user input and then grab the parent of that child node and append another child in it.

**XML FILE:**

```
<policyList>
        <policySecurity>
                <policyName>AutoAdd</policyName>
                <deviceName>PA-722</deviceName>
                <status>ACTIVE</status>
                <srcZone>any</srcZone>
                <dstZone>any</dstZone>
                <srcAddr>5.5.5.5</srcAddr>
                <dstAddr>5.5.5.4</dstAddr>
                <srcUser>any</srcUser>
                <application>any</application>
                <service>htds</service>
                <urlCategory>any</urlCategory>
                <action>deny</action>
        </policySecurity>
        <policySecurity>
                <policyName>Test-1</policyName>
                <deviceName>PA-710</deviceName>
                <status>ACTIVE</status>
                <srcZone>any</srcZone>
                <dstZone>any</dstZone>
                <srcAddr>192.168.1.23</srcAddr>
                <dstAddr>8.8.8.8</dstAddr>
                <srcUser>vivek</srcUser>
                <application>any</application>
                <service>http</service>
                <urlCategory>any</urlCategory>
                <action>deny</action>
        </policySecurity>
</policyList>

```

**C CODE:**

```
int main(){
xmlDocPtr pDoc = xmlReadFile("/var/www/db/db_policy.xml", NULL, XML_PARSE_NOBLANKS | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
    if (pDoc == NULL)
    {
        fprintf(stderr, "Document not parsed successfully.\n");
        return 0;
    }

root_element = xmlDocGetRootElement(pDoc);
if (root_element == NULL)
 {
        fprintf(stderr, "empty document\n");
        xmlFreeDoc(pDoc);
        return 0;
    }

printf("Root Node is %s\n", root_element->name);

        xmlChar* srcaddr = "5.5.5.5";
        xmlChar *xpath = (xmlChar*) "//srcAddr";
        xmlNodeSetPtr nodeset;
        xmlXPathObjectPtr result;
        int i;
        xmlChar *keyword;
        xmlXPathContextPtr context;
        xmlNodePtr resdev;
        xmlChar* resd;
         context = xmlXPathNewContext(pDoc);
        if (context == NULL) {
                printf("Error in xmlXPathNewContext\n");
        }
        result = xmlXPathEvalExpression(xpath, context);
        xmlXPathFreeContext(context);
        if (result == NULL) {
                printf("Error in xmlXPathEvalExpression\n");
        }
         if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
                xmlXPathFreeObject(result);
                printf("No result\n");
        };

        if (result) {
                nodeset = result->nodesetval;
                for (i=0; i < nodeset->nodeNr; i++) {
                        keyword = xmlNodeListGetString(pDoc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
                printf("keyword: %s\n", keyword);
                if(strcmp(keyword, srcaddr) == 0){
                        xmlNodePtr pNode = xmlNewNode(0, (xmlChar*)"service");
                        xmlNodeSetContent(pNode, (xmlChar*)"nonser");
                        xmlAddSibling(result, pNode);
                        printf("added");
                        }
                xmlFree(keyword);
                }
                xmlXPathFreeObject (result);
        }
        xmlFreeDoc(pDoc);
        xmlCleanupParser();
        return (1);
}

```

On running this code, it gets compiled and executed(with a few warnings, but nothing that hinders execution), but it does not add anything to my XML File.