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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Show us your current code.
 

Dharamart

Member
Joined
Jun 25, 2022
Messages
10
Programming Experience
10+
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:

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,436
Location
Norway
Programming Experience
10+
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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
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:
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
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.
 

Dharamart

Member
Joined
Jun 25, 2022
Messages
10
Programming Experience
10+
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:

Dharamart

Member
Joined
Jun 25, 2022
Messages
10
Programming Experience
10+
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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Please put your code and data in code tags. I'll do it one last time...
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Which node do you consider a corrupted node?
 

Dharamart

Member
Joined
Jun 25, 2022
Messages
10
Programming Experience
10+
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:

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,436
Location
Norway
Programming Experience
10+
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.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,436
Location
Norway
Programming Experience
10+
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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
The point of this forum is to help the community learn from each other. Please share how you are removing the UDF from the XML.
 
Top Bottom