-1

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

md ni
  • 121
  • 1
  • 6
  • Does this answer your question? [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c) – Magnetron Jan 17 '23 at 10:55
  • .NET's XML classes (XmlDocument,XmlReader,XDocument) can parse any XML document already. You'll have to access the elements and attributes one by one though – Panagiotis Kanavos Jan 17 '23 at 10:59
  • 1
    An XML document *must* be well-formed though. `` isn't XML, period. By extension, that snippet isn't XML, no matter what the first 5 characters say. No if or but. – Panagiotis Kanavos Jan 17 '23 at 11:02
  • 1
    An XML parser could handle this by skipping characters after the bad input until it found something that's valid to parse, eg it could discard `xxxxxx"` or everything until `/>`. I think this can be done with `XmlReader`. It's far better to just discard the bad document though and ask whoever produced it to send a correct one. There's no way to tell if that `xxxxxx"` was important or not – Panagiotis Kanavos Jan 17 '23 at 11:06

2 Answers2

2

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

Mehrdad Noroozi
  • 620
  • 2
  • 13
0

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);
Skin
  • 9,085
  • 2
  • 13
  • 29