Resolved UDF:data unable to read by c#

Dharamart

Member
Joined
Jun 25, 2022
Messages
10
Programming Experience
10+
I just wanted to read or ignore the <UDF:data>test</UDF:data> from XML in c#. C# read everything from XML except <UDF:data>. please help me this one much needed.
 
Show us your current code.
 
C#:
using System;
using System.Xml;
                 
public class Program
{
    public static void Main()
    {
        var xml = @"<ENVELOPE>
    <HEADER>
        <VERSION>1</VERSION>
        <STATUS>1</STATUS>
    </HEADER>
    <BODY>
        <DATA>
            <MESSAGE>
                <VOUCHER >
                    <GUID>9740ced1-ae14-4acc-be43-d3e250e6bf71-00000005</GUID>
                    <REMOTEALTGUID>9740ced1-ae14-4acc-be43-d3e250e6bf71</REMOTEALTGUID>
                    <UDF:VATDEALERNATURE.LIST DESC="`VATDealerNature`" ISLIST="YES" TYPE="String" INDEX="10031">
                        <UDF:VATDEALERNATURE DESC="`VATDealerNature`">Regular</UDF:VATDEALERNATURE>
                    </UDF:VATDEALERNATURE.LIST>
                </VOUCHER>
            </MESSAGE>
        </DATA>
    </BODY>
</ENVELOPE>";

var document = new XmlDocument();
        document.LoadXml(xml);

        XmlNodeList nodes = doc.GetElementsByTagName("UDF:VATDEALERNATURE.LIST");
         for (int i = 0; i < nodes.Count; i++)
         {
             Console.WriteLine(nodes[i].InnerXml);
             nodes[i].ParentNode.RemoveChild(nodes[i]);
         }
      
            }
}
Not Working.
 
Last edited by a moderator:
Something is missing, "UDF:" is a namespace prefix, if you tried to load such xml string into XmlDocument you would get an error that it is not declared.
 
Assuming that you could load the document as noted by @JohnH , you are doing searches incorrectly. The "UDF" is a namespace. You need to search by tag name and namespace:
 
Yup, as predicted by @JohnH . You need to over come the loading issue before you can move on to your actual problem of removing/ignoring those nodes.

Who provided you that XML? Ask them why they are giving you invalid XML.
 
this XML generated by indian software called tally.
C#:
 XmlNamespaceManager xmlnsManager=new System.Xml.XmlNamespaceManager(doc.NameTable);
                xmlnsManager.AddNamespace("UDF","");

                 XmlNodeList nodes = doc.GetElementsByTagName("UDF:*");
                    foreach (XmlNode xNode in nodes)
                    {
                        xNode.ParentNode.RemoveChild(xNode);
                    }
                    /* for (int i = 0; i < nodes.Count; i++)
                     {
                         Console.WriteLine(nodes[i].InnerXml);
                         nodes.ParentNode.RemoveChild(nodes[i]);
                     }*/
                    if (doc.DocumentElement.Name== "UDF_VATDEALERNATURE.LIST")
                    {
                        string tempxmlstring = doc.DocumentElement.InnerXml;
                        doc.LoadXml(tempxmlstring); 
                    }
 
Last edited by a moderator:
is there any way by which i can remove corrupted node like
C#:
<ENVELOPE>
    <HEADER>
        <VERSION>1</VERSION>
        <STATUS>1</STATUS>
    </HEADER>
    <BODY>
        <DATA>
            <MESSAGE>
                <VOUCHER >
                    <GUID>9740ced1-ae14-4acc-be43-d3e250e6bf71-00000005</GUID>
                    <REMOTEALTGUID>9740ced1-ae14-4acc-be43-d3e250e6bf71</REMOTEALTGUID>
                </VOUCHER>
            </MESSAGE>
        </DATA>
    </BODY>
</ENVELOPE>
using XML to string and vice versa . or any way you can suggest. Its very urgent please help me.
 
Last edited by a moderator:
Please put your code and data in code tags. I'll do it one last time...
 
Which node do you consider a corrupted node?
 
this node i consider a corrupted node.
C#:
<UDF:VATDEALERNATURE.LIST DESC="`VATDealerNature`" ISLIST="YES" TYPE="String" INDEX="10031">
                        <UDF:VATDEALERNATURE DESC="`VATDealerNature`">Regular</UDF:VATDEALERNATURE>
                    </UDF:VATDEALERNATURE.LIST>

I just wanted to remove all node which started with UDF: is it possible.
 
Last edited by a moderator:
You can replace the root element in string representing xml to add the missing namespace declaration, then you can proceed to work with the xml.
 
Do a simple string replace for root node <ENVELOPE> with <ENVELOPE xmlns:UDF="null"> to declare the UDF namespace prefix. After that the xml is valid and can be treated as such. Then you can for example use xpath lookup for //UDF:* nodes and remove each from the document.
 
Back
Top Bottom