Home > Blockchain >  Parsing customized xml in .net
Parsing customized xml in .net

Time:01-22

I have a xml string and when I wanna parse this xml really I cant do it because the structure of this xml is not clear,,,

below is my xml string...

<?xml version="1.0" encoding="UTF-8"?> 
<test1> 
<test2>  
<test3 name="responseCode" value="xxxxx" />  
<test3 name="responseDescription" xxxxxx" />  
</test2>  
</test1>

I really want the c# parser of this xml code,,

can anyone help me? thanks

CodePudding user response:

I don't know where you're getting your XML from but this line ...

<test3 name="responseDescription" xxxxxx" />

... is broken. This parses for me ...

 <test3 name="responseDescription xxxxxx" />

This entire example works ...

var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n<test1> \r\n<test2>  \r\n<test3 name=\"responseCode\" value=\"xxxxx\" />  \r\n<test3 name=\"responseDescription xxxxxx\" />  \r\n</test2>  \r\n</test1>";

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

CodePudding user response:

First I wanna say your xml string is not in a standard format and you need to write your customize parser ,, As I see your xml string the code that can parse your string can be like below:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resulStr); // Load the XML document from the specified file
XmlNodeList response = xmlDoc.GetElementsByTagName("test1");


foreach (XmlNode xn in response)
{
    XmlNodeList records = xn.SelectNodes("test2");
    foreach (XmlNode record in records)
    {
        if (record != null)
        {
            for (int i = 0; i < record.ChildNodes.Count; i  )
            {
                var objfield = record.ChildNodes.Item(i).OuterXml;
                var attributes = Utility.GetSrcInHTMLImgString(objfield);
                var attrkey = attributes[1];
                var attrvalue = attributes[0];
                
            }
        }
    }
    
}

and also the GetSrcInHTMLImgString functon use for parse each test3 field(I use html parser for each test3 field) ,,, below code is the htl parser according to your xml string:

public static List<string> GetSrcInHTMLImgString(string htmlString)
        {
            List<string> srcs = new List<string>();
            string pattern1 = @"(?<=value="").*?(?="")";
            string pattern2 = @"(?<=name="").*?(?="")";
            Regex rgx1 = new Regex(pattern1, RegexOptions.IgnoreCase);
            Regex rgx2 = new Regex(pattern2, RegexOptions.IgnoreCase);
            MatchCollection matches1 = rgx1.Matches(htmlString);
            MatchCollection matches2 = rgx2.Matches(htmlString);
            string value1 = matches1[0].Value;
            string value2 = matches2[0].Value;
            srcs.Add(value1);
            srcs.Add(value2);
            
            return srcs;
        }

I think if you use these code your problem solved,, best regard

  • Related