Solved

# PowerShell to C# XML Help

Posted on 2011-09-24
394 Views
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 Question by:jjthomas3 • 3 • 2 5 Comments LVL 74 Expert Comment It should be as simple as: System.Xml.XmlDocument myXML = new System.Xml.XmlDocument(); myXML.Load("drive:\path\to\myfile.xml");  0 LVL 1 Author Comment 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>


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>  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  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 74 Accepted Solution käµfm³d 👽 earned 500 total points 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(); } } }  Untitled.png 0 LVL 74 Expert Comment What I meant by "automatic interaction" was demonstrated in your code as: t$service.Name


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

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…