我需要找到一种将XML文档分为不同字符串的方法
我有一个传入的字符串,每次都可以更改,字符串为XML。
在服务器从SQL DataTable划痕后,该字符串将从服务器发送。
更多信息:库存是其具有多列的表名,但我使用executereader仅获取一列。
"<Result>\r\n <stock>\r\n <name>cheese</name>\r\n </stock>\r\n <stock>\r\n <name>butter</name>\r\n </stock>\r\n <stock>\r\n <name>MILK</name>\r\n </stock>\r\n <stock>\r\n <name>meat</name>\r\n </stock>\r\n</Result>"
目前,IM正在做的是将其保存到Xmldocument类型中,然后使用方法将其转换为字符串,
private void AddList(XmlDocument xmlDoc)
{
string s = (xmlDoc.DocumentElement.InnerText);
string[] list = s.Split(" ");
int i = 0;
while(i<list.Length)
{
TypeCB.Items.Add(list[i]);// typeCB is a combo box which i want to have the name of the items that were sent
i++;
}
}
此添加cheesebuttermilkmeat作为一个选项,而我希望它是4个不同的选项。
显然,问题是要保存的是一个继续字符串,因此该程序不能将其分开为“”,因为这些程序不存在。
如何将传入文本分开?
i have an incoming string that can change each time, the string is xml.
the string is being sent from a server after the server has exctracted it from a SQL datatable.
some more info: stock is the table name it has several columns but i used executeReader to only get one.
"<Result>\r\n <stock>\r\n <name>cheese</name>\r\n </stock>\r\n <stock>\r\n <name>butter</name>\r\n </stock>\r\n <stock>\r\n <name>MILK</name>\r\n </stock>\r\n <stock>\r\n <name>meat</name>\r\n </stock>\r\n</Result>"
right now what im doing is saving it into an xmlDocument type and then converting it to string using a method
private void AddList(XmlDocument xmlDoc)
{
string s = (xmlDoc.DocumentElement.InnerText);
string[] list = s.Split(" ");
int i = 0;
while(i<list.Length)
{
TypeCB.Items.Add(list[i]);// typeCB is a combo box which i want to have the name of the items that were sent
i++;
}
}
this add cheesebutterMILKmeat as one option while i want it to be 4 diffrent ones.
obviously the problem is that what is being saved is one continues string and so the program cant split it at a " " because those dont exist.
how can i split the incoming text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此示例中,我正在使用
xDocument
从System.xml.linq
namespace,而不是xmldocument
。假设您可以使用
Xdocument
,则可以在stock
节点上循环,然后选择其name
nodes。In this example I am using
XDocument
from theSystem.Xml.Linq
namespace, instead ofXmlDocument
.Assuming you can use
XDocument
, you can loop over thestock
nodes and then select theirname
nodes.