首页 \ 问答 \ 无法使用XDocument获取元素(Not able to get the element with XDocument)

无法使用XDocument获取元素(Not able to get the element with XDocument)

下面是我用来获取“人物”的“名称”(属性)值的代码

parameter tag =“person”和parameter attribute =“name”

public static string GetInformationFromXML(string tag,
                                           string attribute,
                                           string filePath)
{
        XDocument doc = XDocument.Load(filePath);
        string info = doc.Element(tag).Attribute(attribute).Value;

        return info;
}

doc.Element(tag)没有得到元素,即使我扩展doc时,它确实有一个元素类型为'Element'而名称'person'正在读取的文件是XmlDocument供您参考。

下面是我想要阅读的文件的xml

<?xml version="1.0"?>
<import>
  <company name1="ABC" name2="" action="create" 
           profile="\Profiles\ABC\" id="C1">
    <address street="industrial" city="london" 
             country="england" id="A1">
      <telecom type="phone" value="4839282992" 
               desc="" default="true" />
      <telecom type="fax" value="3232" desc="" />
    </address>
  </company>
  <person title="Mr." name="Tariq" surname="sheikh" 
          lang="EN" action="create"  profile="Profiles\Tariq" 
          login="tariq" password="123456" default_address="A1">
    <link reference="C1" type="Employee" description="Software developer" />
    <address street="baker street" zip="12443" 
             city="london" country="england" />
    <account bank="Barclays" account="4378734834" />
    <telecom type="email" value="tariq.sheikh@abc.co.in" desc="" />
    <registration type="temporaryID" 
                  value="4623648c-739e-49c8-93fa-41dc7fed53ea" />
  </person>
</import>

我是XDocument的新手!


Below is the code i am using to get the value of the 'name'(attribute) of the 'person'

parameter tag="person" and parameter attribute="name"

public static string GetInformationFromXML(string tag,
                                           string attribute,
                                           string filePath)
{
        XDocument doc = XDocument.Load(filePath);
        string info = doc.Element(tag).Attribute(attribute).Value;

        return info;
}

doc.Element(tag) is not getting the element even though when i expand doc, it does have an element with type 'Element' and Name 'person' the file being read is XmlDocument for your information.

below is the xml of the file i am trying to read

<?xml version="1.0"?>
<import>
  <company name1="ABC" name2="" action="create" 
           profile="\Profiles\ABC\" id="C1">
    <address street="industrial" city="london" 
             country="england" id="A1">
      <telecom type="phone" value="4839282992" 
               desc="" default="true" />
      <telecom type="fax" value="3232" desc="" />
    </address>
  </company>
  <person title="Mr." name="Tariq" surname="sheikh" 
          lang="EN" action="create"  profile="Profiles\Tariq" 
          login="tariq" password="123456" default_address="A1">
    <link reference="C1" type="Employee" description="Software developer" />
    <address street="baker street" zip="12443" 
             city="london" country="england" />
    <account bank="Barclays" account="4378734834" />
    <telecom type="email" value="tariq.sheikh@abc.co.in" desc="" />
    <registration type="temporaryID" 
                  value="4623648c-739e-49c8-93fa-41dc7fed53ea" />
  </person>
</import>

I am new to XDocument !


原文:https://stackoverflow.com/questions/15403678
更新时间:2022-04-03 18:04

最满意答案

您可以执行软重置回主分支的头部,这将维护您当前的工作目录,但将分支和索引重置为主。

git reset --soft master

一切都会恢复到你提交之前的状态。 然后您可以添加单个文件。

git add <file>

添加个别文件的一部分。

git add --patch <file>

或者全身心投入并使用完整的交互式添加。

git add --interactive

有关深入指南,请参阅Pro Git Book中的交互式分段的完整章节。


You can do a soft reset back to the head of the master branch, which will maintain your current working directory but reset the branch and index back to master.

git reset --soft master

Everything will be back the way it was before your commit. You can then add individual files.

git add <file>

Add parts of individual files.

git add --patch <file>

Or go whole hog and use full interactive add.

git add --interactive

See the full chapter on interactive staging in the Pro Git Book for an in depth guide.

相关问答

更多
  • https://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html Cherry-pick会创建新的提交,因此如果省略分支中不存在但存在于分支中的文件,您将要合并,最终不会发生任何事情。 mkdir test && cd test && git init && touch foo.txt git add foo.txt && git commit -m "Init repo" git branch test && git checkou ...
  • 我会选择引入所需部分的提交,然后git reset HEAD^以删除最新的提交并将更改保留在工作副本中,然后git add -p您想要的部分并从中进行新的提交。 I would cherry-pick the commit that introduces the parts you want, then git reset HEAD^ to remove the most recent commit and leave the changes in the working copy, then git ad ...
  • 您可以执行软重置回主分支的头部,这将维护您当前的工作目录,但将分支和索引重置为主。 git reset --soft master 一切都会恢复到你提交之前的状态。 然后您可以添加单个文件。 git add 添加个别文件的一部分。 git add --patch 或者全身心投入并使用完整的交互式添加。 git add --interactive 有关深入指南,请参阅Pro Git Book中的交互式分段的完整章节。 You can do a soft reset back to ...
  • 如果它们是作为两个单独的提交完成的话,那么不应该这样做,因为这些文件将在一个修订版本中被删除,并且在新版本中创建一个新文件。 如果你想保留重命名,你应该做一个git commit --amend来追加你的添加到前一个提交的删除。 Not if they're done as two separate commits, because the files will have been deleted in one revision and a new file created in the new revis ...
  • 为什么开发人员需要推送omni-backup回购? 出于备份目的,我宁愿将不同开发人员的存储库注册为远程存储库,并在所有远程存储库上每晚(从omni-backup服务器)执行git fetch 。 这样,没有分支名称可能串通。 一个更自动化的过程(开发人员不必明确地推送他/她不直接使用的回购中的任何内容,但只考虑备份) 然后我会从omni-backup生成一个漂亮的小git archive并将其存储起来。 Why would you need for developer to push to omni-ba ...
  • 您可以将这些提交转换为修补程序,手动编辑修补程序(用新路径替换旧路径)然后应用。 $ git init Initialized empty Git repository in /tmp/gitt/.git/ $ mkdir olddir $ cp /home/vi/code/_/ucontext.cpp olddir/ucontext.cpp $ git add . $ git commit -m "initial commit" [master (root-commit) c0bf371] initial ...
  • 很高兴帮助:)如果master中的那个将是永久的方式,我会修复主题分支中的文件结构以匹配master,提交到分支,* 然后将更改合并到master,而不是cherry-pick。 如在 git checkout branch mv other/subdir/file subdir/file git status // git add and git rm to get the commit proper git commit -m "fixing file struct" git checkout mast ...
  • 这是常见行为,已在文档中进行了描述。 首先,为什么你的复制不会造成合并冲突 ? 在合并期间,将更新工作树文件以反映合并的结果。 在对共同祖先版本所做的更改中, 非重叠版本(即,您更改文件的某个区域而另一侧保持该区域完整,反之亦然)将逐字汇入最终结果中。 同一段落中的下一句话描述了为什么在没有--rebase情况下将origin --rebase您的主人--rebase来创建合并冲突: 然而,当双方对同一区域进行更改时,Git不能随意选择一方而不是另一方,并要求您通过将双方所做的事留在该区域来解决它。 合并冲 ...
  • 我假设你只需要将该文件重置为master上的版本,而不是删除任何更改历史记录,对吗? 前者很容易,后者需要修改历史记录,不应轻易做到(特别是如果你把分支推到当地回购之外)。 要将文件foo/bar/baz.txt的内容重置为foo/bar/baz.txt上的状态,只需检查分支并运行 git checkout master -- foo/bar/baz.txt 这将从master获取文件,将其写入工作副本,并在索引中暂存。 你现在可以只是git commit ,你的文件现在将匹配master 。 I ass ...
  • 概要 不能仅合并提交的一部分。 但是,您可以在C5之前reset (软或混合)提交,在两个单独的提交中提交更改和删除,并在其上应用您的其他提交。 但是,这将更改您的分支历史记录,因此如果您将分支推送到远程和/或其他任何人在您的提交之上工作, 则不应该这样做。 细节 第一个可取的步骤,就像你想弄乱你的分支机构的历史一样,就是创建一个备用分支,以防一切都向南。 git branch backup_branch做到这一点。 如果你搞砸了所有东西,你可以git checkout backup_branch并在你的旧 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)