我需要找到一种将XML文档分为不同字符串的方法

发布于 2025-01-23 10:08:16 字数 1053 浏览 2 评论 0原文

我有一个传入的字符串,每次都可以更改,字符串为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-01-30 10:08:16

在此示例中,我正在使用xDocumentSystem.xml.linq namespace,而不是xmldocument

假设您可以使用Xdocument,则可以在stock节点上循环,然后选择其name nodes。

// do something like this instead of how you create your XmlDocument:
string xml = "<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>";

XDocument doc = XDocument.Parse(xml);

//Once you have the parsed xml you can select the node values and add them to the combobox
foreach (XElement el in doc.Descendants("stock"))
{
    string name = el.Element("name").Value;
    TypeCB.Items.Add(name);
}

In this example I am using XDocument from the System.Xml.Linq namespace, instead of XmlDocument.

Assuming you can use XDocument, you can loop over the stock nodes and then select their name nodes.

// do something like this instead of how you create your XmlDocument:
string xml = "<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>";

XDocument doc = XDocument.Parse(xml);

//Once you have the parsed xml you can select the node values and add them to the combobox
foreach (XElement el in doc.Descendants("stock"))
{
    string name = el.Element("name").Value;
    TypeCB.Items.Add(name);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文