Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
XML
<?xml version="1.0" standalone="yes"?>
<EnDic11>
  <EnDic>
    <word>ant</word>
    <meanings>
      <meaning>test1 </meaning>
      <meaning> test2 </meaning>
      <meaning> test3</meaning>
          </meanings>
  </EnDic>
  <EnDic>
    <word>cat</word>
    <meanings>
      <meaning>test4</meaning>
    </meanings>
  </EnDic>
 </EnDic11>


I try to get,

ant-test1,test2,test3
cat-test4

as the output.

But I got errors.
How to solve the problem.
THANK YOU!

What I have tried:

VB
Dim i As Integer
        Dim output  as string=""
        Dim xl As String
        Dim ac As XmlDocument = Nothing
        Dim xn As XmlNodeList
        Dim [FileStream] As New FileStream(enSinDicPath, FileMode.Open, FileAccess.Read)
        [XmlDocument].Load([FileStream])
        [XmlNodeList] = [XmlDocument].GetElementsByTagName("EnSinDic")

        For i = 0 To [XmlNodeList].Count - 1
            [XmlNodeList](i).ChildNodes.Item(0).InnerText.Trim()

            For Each word In words
                If [XmlNodeList](i).ChildNodes.Item(0).InnerText.ToLower = word.ToLower Then
                    xl = [XmlNodeList](i).InnerXml
                    ac.Load(xl)
                    xn = ac.GetElementsByTagName("meanings")
                    For q = 0 To xn.Count - 1
                       output = output & Environment.NewLine & xn(i).ChildNodes.Item(0).InnerText.ToLower
                    Next
                End If
            Next
        Next
    End Sub
Posted
Updated 19-Mar-20 22:40pm
Comments
CHill60 19-Mar-20 5:01am    
You need to tell us what the errors are
ZurdoDev 19-Mar-20 7:58am    
Nope! It is unlikely anyone will go through your code and try to figure out how to make it do what you want.
Richard Deeming 19-Mar-20 12:34pm    
The obvious problem is that you call GetElementsByTagName("EnSinDic"), whereas the XML document you've shown contains no such element.

Beyond that, you need to tell us what the errors are.

Click the green "Improve question" link and update your question to add the full details of the errors. Remember to indicate which line of code they relate to.

1 solution

Check this:
C#
string xcontent = @"<?xml version='1.0' standalone='yes'?>
<EnDic11>
  <EnDic>
    <word>ant</word>
    <meanings>
      <meaning>test1</meaning>
      <meaning>test2</meaning>
      <meaning>test3</meaning>
          </meanings>
  </EnDic>
  <EnDic>
    <word>cat</word>
    <meanings>
      <meaning>test4</meaning>
    </meanings>
  </EnDic>
 </EnDic11>";
 
 
 XDocument xdoc = XDocument.Parse(xcontent);
 
 var result = xdoc.Descendants("EnDic")
 	.Select(x=>string.Concat(x.Element("word").Value, "-", string.Join(",", x.Descendants("meaning").Select(y=>y.Value))))
	.ToList();


result:
ant-test1,test2,test3 
cat-test4 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900