English 中文(简体)
选择xml不退还任何结果
原标题:select-xml does not return any results
  • 时间:2010-12-09 10:19:56
  •  标签:
  • powershell

I m试图利用权力指挥选择Xml从Xml档案中挑选数据。 在我指挥下,没有结果或错误被退回。 我预计,这些版本的清单将归还给屏幕。 这有什么错误?

PS C:> select-xml -path "C:	.xml" -xpath "//edition" | foreach {$_.node.InnerXML}
PS C:>

The XML file (C: .xml) is:

 <?xml version="1.0" encoding="utf-8"?>
  <Book>
    <projects>
      <project name="Book1" date="2009-01-20">
        <editions>
           <edition language="English">En.Book1.com</edition>
           <edition language="German">Ge.Book1.Com</edition>
           <edition language="French">Fr.Book1.com</edition>
           <edition language="Polish">Pl.Book1.com</edition>
        </editions>
      </project>
    </projects>
  </Book>
最佳回答

因此,我知道什么是错误的。 在《XML宣言》第1节之前有一个白色空间。 确实是一句空话。 第一行:

PS H:> $xml = ([xml](Get-Content -Path C:scripts	.xml)).Book.Projects.Project

它错误如下:

"..The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it..."

我确信,在试图阅读Xml文档时,选择xml在问题中应当失败。 xml案在IE进行了罚款。

问题回答

这样做的另一个途径是:

PS H:> $xml = ([xml](Get-Content -Path C:scripts	.xml)).Book.Projects.Project
PS H:> $xml.editions | select -ExpandProperty Edition

language                                                    #text
--------                                                    -----
English                                                     En.Book1.com
German                                                      Ge.Book1.Com
French                                                      Fr.Book1.com
Polish                                                      Pl.Book1.com

1 P-4, 1 P-3, 1 P-2, 1 GS Xml.XmlElement, 没有内心 Xml 财产

Select-Xml -Path C:	.xml -XPath "//edition" | 
    % {$_.node} | get-member | out-gridview

这里还有另外几个选择:

$xml = [xml]@ 
<?xml version="1.0" encoding="utf-8"?>   
<Book>
  <projects>
    <project name="Book1" date="2009-01-20">
      <editions>
        <edition language="English">En.Book1.com</edition>
        <edition language="German">Ge.Book1.Com</edition>
        <edition language="French">Fr.Book1.com</edition>            
        <edition language="Polish">Pl.Book1.com</edition>         
      </editions>       
    </project>     
  </projects>   
</Book> 
 @

$xml | Select-Xml  //edition/text()  | Foreach {"$_"}

$xml | Select-Xml  //edition  |  Foreach {$_.Node.InnerText}




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签