PowerShell的C#等效代码

问题描述:

如何将下面的代码转换为C#? 我有一个XML字符串,我需要找到属性名称,同时检查是否存在具有“别名”的父节点。PowerShell的C#等效代码

$atts = $xml.GetElementsByTagName('attribute'); 
foreach($att in $atts) 
{ 
    if($att.ParentNode.HasAttribute('alias')) 
    { 
     $attName = $att.ParentNode.GetAttribute('alias') + "." + $att.name 
     Write-Output $att.ParentNode.GetAttribute('alias') + "." + $att.name 
    } 
    else 
    { 
     $attName = $att.name 
     Write-Output $att.name 
    } 
} 
+5

你尝试过什么?堆栈溢出不是免费的代码转换服务。 – CodeCaster

namespace * 
{ 
    using System; 
    using System.Xml; 

    class XmlTest 
    { 
     public static void yourTest() 
     { 
      XmlDocument xmlDocument = new XmlDocument(); 
      xmlDocument.Load("yourXmlPath.xml"); 

      // It is recommended that you use the XmlNode.SelectNodes or XmlNode.SelectSingleNode method instead of the GetElementsByTagName method. 
      XmlNodeList xmlNodes = xmlDocument.GetElementsByTagName("attribute"); 

      foreach(XmlNode xmlNode in xmlNodes) 
      { 
       if (xmlNode.ParentNode.Attributes.GetNamedItem("alias") != null) 
       { 
        string attributeName = xmlNode.ParentNode.Attributes.GetNamedItem("alias").InnerText + "." + xmlNode.GetNamedItem("name").InnerText; 
        Console.WriteLine(attributeName); 
       } 
       else 
       { 
        string attributeName = xmlNode.Attributes.GetNamedItem("name").InnerText; 
        Console.WriteLine(attributeName); 
       } 
      } 
     } 
    } 
}