解析 C# 中的可选标签

发布于 2024-10-20 03:58:35 字数 1602 浏览 8 评论 0原文

我正在尝试找到一种简单而灵活的方法来满足以下要求。

我有一条采用这种安排的 XML 消息:

  <persons>
    <person>
       <firstName>Mike</firstName>
       <middleName>K.</middleName>
       <lastName>Kelly</lastName>
    </person>
    <person>
       <firstName>Steve</firstName>
       <lastName>David</lastName>
    </person>
    <person>
       <firstName>Laura</firstName>
       <middleName>X.</middleName>
       <lastName>Xavier</lastName>
    </person>
  </persons>

我想使用 xPath 表达式解析此 XML。

persons/person/firstName
persons/person/middleName
persons/person/lastName

我的目标是将这样的firstName、middleName 和lastName 标记值存储到这样的字符串对象列表中:

firstNameList[0] = "Mike";
firstNameList[1] = "Steve";
firstNameList[2] = "Laura";

middleNameList[0] = "K.";
middleNameList[1] = null;
middleNameList[2] = "X.";

lastNameList[0] = "Kelly";
lastNameList[1] = "David";
lastNameList[2] = "Xavier";

在我的C# 代码中,我这样做:

XmlNodeList firstNameNodeList = xmlDoc.SelectNodes("persons/person/firstName", nsmgr);
XmlNodeList middleNameNodeList = xmlDoc.SelectNodes("persons/person/middleName", nsmgr);
XmlNodeList lastNameNodeList = xmlDoc.SelectNodes("persons/person/lastName", nsmgr);

这段代码的问题是,对于中间名,我没有第二个我的 XML 列表中的人。因此 middleNameNodeList 返回 2 个值(K. 和 X.),但我不知道是否缺少第一个、第二个或第三个人的中间名。

我希望 SelectNodes() API 能够提供迭代或索引 ID,因为重复元素具有给定值。

请建议我一种最简单的方法来实现我所需要的?非常感谢你的帮助,JK

I'm trying to find an easy and slick way to do the following requirement.

I have a XML message with this arrangement:

  <persons>
    <person>
       <firstName>Mike</firstName>
       <middleName>K.</middleName>
       <lastName>Kelly</lastName>
    </person>
    <person>
       <firstName>Steve</firstName>
       <lastName>David</lastName>
    </person>
    <person>
       <firstName>Laura</firstName>
       <middleName>X.</middleName>
       <lastName>Xavier</lastName>
    </person>
  </persons>

I want to parse this XML using xPath expressions.

persons/person/firstName
persons/person/middleName
persons/person/lastName

My objective is store firstName, middleName and lastName tag values like this into a list of string objects like this:

firstNameList[0] = "Mike";
firstNameList[1] = "Steve";
firstNameList[2] = "Laura";

middleNameList[0] = "K.";
middleNameList[1] = null;
middleNameList[2] = "X.";

lastNameList[0] = "Kelly";
lastNameList[1] = "David";
lastNameList[2] = "Xavier";

In my C# code, I do this:

XmlNodeList firstNameNodeList = xmlDoc.SelectNodes("persons/person/firstName", nsmgr);
XmlNodeList middleNameNodeList = xmlDoc.SelectNodes("persons/person/middleName", nsmgr);
XmlNodeList lastNameNodeList = xmlDoc.SelectNodes("persons/person/lastName", nsmgr);

The problem with this code is that for middle name, I don't have it for 2nd person in my XML list. So the middleNameNodeList returns 2 values (K. and X.) but I wouldn't know whether the 1st or 2nd or 3rd person's middle name is missing.

I was hoping that SelectNodes() API would provide an iteration or index ID as which repeating element has a given value.

Please suggest me an easiest way to achieve what I needed? Thanks so much for your help, JK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

冬天旳寂寞 2024-10-27 03:58:35

这个怎么样?

foreach (Node person in xmlDoc.SelectNodes("persons/person", nsmgr))
{
    firstNameNodeList.Add(person.SelectSingleNode("firstName", nsmgr));
    middleNameNodeList.Add(person.SelectSingleNode("middleName", nsmgr));
    lastNameNodeList.Add(person.SelectSingleNode("lastName", nsmgr));
}

How about this?

foreach (Node person in xmlDoc.SelectNodes("persons/person", nsmgr))
{
    firstNameNodeList.Add(person.SelectSingleNode("firstName", nsmgr));
    middleNameNodeList.Add(person.SelectSingleNode("middleName", nsmgr));
    lastNameNodeList.Add(person.SelectSingleNode("lastName", nsmgr));
}
彩虹直至黑白 2024-10-27 03:58:35

尝试获取人员列表,而不是获取姓名列表,然后迭代该列表并获取他们的姓名。

Intead of getting a list of names, try getting a list of persons, then iterate the list and get their names.

拧巴小姐 2024-10-27 03:58:35

您只需迭代 persons/person 并单独处理每个人 - 这将起作用:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"test.xml");
var persons = xmlDoc.SelectNodes("persons/person");
foreach (XmlNode person in persons)
{
    string firstName = person.SelectSingleNode("firstName").InnerText;
    string middleName = (person.SelectSingleNode("middleName") != null) 
                        ? person.SelectSingleNode("middleName").InnerText 
                        : null;
    string lastName = person.SelectSingleNode("lastName").InnerText;
}

You just have to iterate over persons/person and handle each individually - this would work:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"test.xml");
var persons = xmlDoc.SelectNodes("persons/person");
foreach (XmlNode person in persons)
{
    string firstName = person.SelectSingleNode("firstName").InnerText;
    string middleName = (person.SelectSingleNode("middleName") != null) 
                        ? person.SelectSingleNode("middleName").InnerText 
                        : null;
    string lastName = person.SelectSingleNode("lastName").InnerText;
}
故人的歌 2024-10-27 03:58:35

尝试

   <persons>
    <person>
       <firstName>Mike</firstName>
       <middleName>K.</middleName>
       <lastName>Kelly</lastName>
    </person>
    <person>
       <firstName>Steve</firstName>
       <middleName /> 
       <lastName>David</lastName>
    </person>
    <person>
       <firstName>Laura</firstName>
       <middleName>X.</middleName>
       <lastName>Xavier</lastName>
    </person>
  </persons>

<person>
   <firstName>Steve</firstName>
   <middleName /> 
   <lastName>David</lastName>
</person>

此操作应该为 InnnerText 返回“K”、“”、“X”

Try

   <persons>
    <person>
       <firstName>Mike</firstName>
       <middleName>K.</middleName>
       <lastName>Kelly</lastName>
    </person>
    <person>
       <firstName>Steve</firstName>
       <middleName /> 
       <lastName>David</lastName>
    </person>
    <person>
       <firstName>Laura</firstName>
       <middleName>X.</middleName>
       <lastName>Xavier</lastName>
    </person>
  </persons>

<person>
   <firstName>Steve</firstName>
   <middleName /> 
   <lastName>David</lastName>
</person>

this should return "K","","X" for InnnerText

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文