【问题标题】:Get XML Element and it's children, and then the values of those children, by name获取 XML 元素及其子元素,然后按名称获取这些子元素的值
【发布时间】:2015-07-27 02:44:23
【问题描述】:

我是使用 xml 的新手,我开始感到沮丧,做我需要的事情是多么困难,而且我在 google 上找到的所有东西都不起作用。

<Player>
<Name>PoopNUG</Name>
<PlayerTag>DANKMEMES</PlayerTag>
<StartStats>
  <StartNum>2</StartNum>
  <LastUpdated>2015-07-24T22:21:30.5463885-05:00</LastUpdated>
</StartStats>
<CurrentStats>
  <CurrentNum>3</CurrentNum>
  <LastUpdated>2015-07-24T22:21:30.5463885-05:00</LastUpdated>
</CurrentStats>

这是我正在使用的 xml 示例。文件中有很多玩家。我需要按名称查找玩家,然后访问该玩家的子项,并通过要使用的元素名称进行操作。

我可以使用以下方法提取正确的元素:

IEnumerable<XElement> myPlayers =
            from myP in pElements.Elements("Player")
            where (string)myP.Element("Name") == pNameCombo.SelectedItem.ToString()
            select myP;

但在那之后我迷路了。我已经尝试了大约 20 种我在 google 上找到的方法,但没有任何方法可以获取 myPlayers 中的数据。我可以将整个内容打印出来,但无法通过名称和这些元素的值访问单个元素。

有什么帮助吗?

【问题讨论】:

  • 也许在 google 上搜索并不是学习新语言的最佳策略。你有没有想过买一本书?

标签: c# xml


【解决方案1】:

我没有运行代码,但你可以尝试这样的方法,根据元素名称和属性名称的值来搜索你的元素。当然你也可以直接使用 LINQ

XElement Root = System.Xml.Linq.XElement.Load("MyFile.xml");
foreach(XElement ChildOfRoot in Root.Elements())
{
     //loop for eleemnt name player/Player
     if(ChildOfRoot.Name.LocalName.ToLower() == "player")
     {
         //compare the of the player.
         if(ChildOfRoot.Attribute("Name").Value == "MyName")
         {
             //once you found the player, loop thorugh its children
              foreach(XElement ChildOfPlayer in ChildOfRoot.Elements())
              {

                      //read the value of child nodes here
               }
               break;
           }
       }
    }

【讨论】:

    【解决方案2】:

    这个对我有用

    XElement xmlDoc = XElement.Load(@"C:\Users\username\Desktop\Noname1.xml");
    
            IEnumerable<XElement> myPlayers = from myP in xmlDoc.Elements("Player")
                                              where (string)myP.Element("Name") == "PoopNUG"
                                              select myP;
    
            foreach (XElement player in myPlayers)
            {
                string name = player.Element("Name").Value;
                string tag = player.Element("PlayerTag").Value;
            }
    

    xml文件是

    <?xml version="1.0" encoding="utf-8"?>
    <root>
    <Player>
    <Name>PoopNUG</Name>
    <PlayerTag>DANKMEMES</PlayerTag>
    <StartStats>
      <StartNum>2</StartNum>
      <LastUpdated>2015-07-24T22:21:30.5463885-05:00</LastUpdated>
    </StartStats>
    <CurrentStats>
      <CurrentNum>3</CurrentNum>
      <LastUpdated>2015-07-24T22:21:30.5463885-05:00</LastUpdated>
    </CurrentStats>
    </Player>
    <Player>
    <Name>PoopNUG</Name>
    <PlayerTag>DANKMEMES</PlayerTag>
    <StartStats>
      <StartNum>2</StartNum>
      <LastUpdated>2015-07-24T22:21:30.5463885-05:00</LastUpdated>
    </StartStats>
    <CurrentStats>
      <CurrentNum>3</CurrentNum>
      <LastUpdated>2015-07-24T22:21:30.5463885-05:00</LastUpdated>
    </CurrentStats>
    </Player>
    </root>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多