Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell to C# XML Help

Posted on 2011-09-24
5
Medium Priority
?
408 Views
Last Modified: 2012-05-12
I just recently got into using PowerShell because it is quite amazing for, well, just about anything. And it's easy to learn, I do have a need to use c# time to time however, as I am part of a much larger project,  Is there something as simple as this PowerShell Call in C#

[xml]$myXML = Get-Content drive:\path\to\myfile.xml
0
Comment
Question by:jjthomas3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36593955
It should be as simple as:
System.Xml.XmlDocument myXML = new System.Xml.XmlDocument();

myXML.Load("drive:\path\to\myfile.xml");

Open in new window

0
 
LVL 1

Author Comment

by:jjthomas3
ID: 36713152
In PowerShell if I delcare

[xml]$myXML = Get-Content drive:\path\to\myfile.xml

And myFile.XML looks like this one here:

<?xml.....   >
<application name="my Application">
  <connections>234</connections>
  <lastUser>Mr. Sinatra</lastUser>
</application>

Open in new window


I do not have to write any more code to directly interact with the XML..... so that if I were to output the value of $myXML.Application.Name the return would be "my Application". Likewise, if I had the following XML file, services.xml:
 
<?xml....>
<services>
    <service>
         <name>SampleService01</spooler>
         <description>Windows Sample Service 01</description>
    </service>
    <service>
         <name>SampleService02</spooler>
         <description>Windows Sample Service 02</description>
    </service>
    <service>
         <name>SampleService03</spooler>
         <description>Windows Sample Service 03</description>
    </service>
    <service>
         <name>SampleService04</spooler>
         <description>Windows Sample Service 04</description>
    </service>
</services>

Open in new window


I could simply perform the following foreach loop
 
[xml]$services= Get-Content drive:\path\to\services.xml
foreach ($service in $services) {
     Write-Host "Service Name:`t$service.Name"
     Write-Host "Description :`t$service.Description"
     ......perform other actions or logic against service....
}

//Or even access the members/nodes directly

Start-Service $services.Services.Service[1].Name

Open in new window



I don't have to write any additional code to parse the data out of the XML File. I was hoping for something as simple in C# with having to iterate through NodeTypes, or using Reader.Read() with switch statements etc.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 36714637
I don't believe you can get that much automatic interaction, at least not without some initial scaffolding, but I also don't believe the code to interact with the file is that much different. Given you last code post ( http:#codeSnippet8210165 ), I would envision an equivalent in C# to be:

namespace _27326029
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Xml.XmlDocument myXML = new System.Xml.XmlDocument();

            myXML.Load(@"input.xml");

            foreach (System.Xml.XmlNode service in myXML.SelectNodes("//service"))
            {
                System.Console.WriteLine("Service Name: {0}", service.SelectSingleNode("name").InnerText);
                System.Console.WriteLine("Description: {0}\n", service.SelectSingleNode("description").InnerText);
            }

            System.Console.ReadKey();
        }
    }
}

Open in new window



Untitled.png
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36714648
What I meant by "automatic interaction" was demonstrated in your code as:

t$service.Name

Open in new window


It appears that PS actually creates an underlying object for you with the property names set to the node names. You'll notice above that I had to refer to the nodes using strings (in the Select*Node* calls). You can get something similar to what you have, but it would rely on the aforementioned "scaffolding".
0
 
LVL 1

Author Closing Comment

by:jjthomas3
ID: 36894582
PowerShell makes it much easier, but unfortunately there's no automatic serialization/deserialization. So, that having been said, there's not much more code  than what I was doing in PS. Thanks.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question