如何使用Substring()方法读取我的Xml字符串 [英] How Can I Read My Xml String Using Substring() Methods
问题描述
你怎么能用substring或其他方法读取我的xml字符串。
i想读帅哥,仍然可以制作和SuperHandsome字符串
Hi how can i read my xml string using substring or other methods.
i want to read "Handsome", "Still Can Make it" and "SuperHandsome" string
<male>
<gender>Tommy = Handsome</gender>
<gender>Jackson = Handsome</gender>
<gender>David = SuperHandsome</gender>
<gender>Chris = Still Can Make it</gender>
</male>
Try
Using reader As XmlReader = XmlReader.Create("C:\Users\Desktop\Boy.xml")
While reader.Read()
If reader.IsStartElement() Then
Select Case reader.Name
Case "gender"
If reader.Read() Then
If reader.Value.Trim() <> Nothing Then
boyname += reader.Value.Trim() & Environment.NewLine
End If
End If
End Select
End If
End While
End Using
Console.ReadLine()
Catch es As Exception
End Try
MsgBox(boyname)
输出:
Tommy =帅哥
杰克逊=帅哥
David = SuperHandsome
克里斯=仍然可以成功
我怎样才能读完等于=字符串
i希望输出显示在下面
输出:
帅气
帅哥
SuperHandsome
仍然可以使它
Output:
Tommy = Handsome
Jackson = Handsome
David = SuperHandsome
Chris = Still Can Make it
how can i read after the equal "=" string
i want the output to display below
Output:
Handsome
Handsome
SuperHandsome
Still Can Make it
推荐答案
你的XML做得不好。如果您的XML看起来像这样会更好:
Your XML is poorly done. It would have been better had your XML looked something like this:
<persons gender="male">
<person name="Tommy" value="Handsome" />
<person name="Jackson" value="Handsome" />
<person name="David" value="SuperHandsome" />
<person name="Chris" value="Still can make it" />
</persons>
这样就不需要解析其中包含的字符串了。
但是,这取决于你。使用 myStringValue .IndexOf(=)
获取=字符的位置。然后,您可以使用该位置值来获取=字符左侧所有内容的 SubString()
,并再次作为获取其后所有内容的起点。
阅读这两种方法及其工作方式这里 [ ^ ]。
That way there's no need to do any parsing of a string contained within.
But, that's up to you. Get the position of the "=" character with myStringValue.IndexOf("=")
. You can then use that position value to get the SubString()
of everything to the left of the "=" character and again as a starting point to get everything after it.
Read up on the two methods and how they work here[^].
这篇关于如何使用Substring()方法读取我的Xml字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!