2

I have a C# application that uses a button to generate a file. Currently, I want to use C# to extract out contents from the XML file and pass it as a string. For example in my XML file, I have a tag name. I want to use c# to extract the name from the XML file. How should I go about achieving it? Below is the sample code I have currently. The entire process must be carried out using a button click.

private void button1_Click(object sender, EventArgs e)
{
    XElement xml = XElement.Load("C:\\Windows 7.xml"); 
    IEnumerable<XElement> propertyIDs = xml.Descendants("PropertyId");

    foreach (XElement child in xml.Elements())
    {
        XElement row = child.Element("my:VM_Name");
        string test = xml.ToString();
        Console.WriteLine(test);
    }    
}

Please access this link to view my xml file: http://pastebin.com/NKhBb4Zh

dwyane
  • 127
  • 2
  • 4
  • 8
  • Your code looks pretty good. I don't understand the question; what is it you want to have happen in the body of your `foreach` loop? – Kirk Woll Jul 06 '11 at 03:11
  • What are you trying to do that doesn't work? Do you get an exception? What doesn't look right? – Tipx Jul 06 '11 at 03:13
  • @Kirk Woll using Console.WriteLine(xml.Value); I am able to display the contents in my xml file. I want to extract the contents for each elements and pass it to a string in c#. – dwyane Jul 06 '11 at 03:14
  • I want to extract out an XML content from an XML file and parse it to a string. – dwyane Jul 06 '11 at 03:16
  • @dwayne, define "contents". Do you mean the outer-xml? Do you mean the inner-xml? The text content? Or the value of some or all of the attributes? – Kirk Woll Jul 06 '11 at 03:19
  • If you don't mind editing your question and add some of the XML you're trying to parse, what you currently get and what you'd want, it would be clearer and I'm sure you'd get a very helpful answer within a few mins! :-D – Tipx Jul 06 '11 at 03:26
  • @Kirk Woll I added this code XElement name = xml.Element("my:VM_Name"); string test = xml.ToString(); Console.WriteLine(test); But I am still unable to extract out Windows 7 from the XML tag. – dwyane Jul 06 '11 at 03:32
  • @Kirk Woll i get this error The ':' character, hexadecimal value 0x3A, cannot be included in a name. – dwyane Jul 06 '11 at 03:38
  • The `my` part is the XML namespace. It is used differently. See this article: http://msdn.microsoft.com/en-us/library/bb943852.aspx – Nithin Philips Jul 06 '11 at 03:48

2 Answers2

2

I rewrote your example and changed it to make use of the XmlDocument class. As there is the my Namespace I had to add a NameSpaceManager. using this you may even select a spefic node.

        string url = @"e:\temp\data.xml";

        XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(url);
        XmlElement docElement = doc.DocumentElement;

        /// loop through all childNodes
        foreach (XmlNode childNode in docElement.ChildNodes)
        {
            Console.WriteLine(childNode.Name + ": " + childNode.InnerText);
        }

        XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
        mgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-05-27T03:57:48");

        /// use the given XmlNamespaceManager to select a specific element
        XmlNode node = docElement.SelectSingleNode("my:VM_DiskSize", mgr);
        /// use innerText for node text and value for attributes only
        Console.WriteLine("\n" + node.Name + ": " + node.InnerText);

hth

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
1

The comments you added to your question were very helpful. In particular:

I added this code:

XElement name = xml.Element("my:VM_Name"); 
string test = xml.ToString(); 
Console.WriteLine(test); 

But I am still unable to extract out Windows 7 from the XML tag

And:

i get this error The ':' character, hexadecimal value 0x3A, cannot be included in a name.

Let's start with the error first. You cannot pass to the Element method an ns:name pair as you've done. With this API, the namespace (ns) must be supplied programatically via the XName type. So instead, that line should read:

XElement name = xml.Element(XName.Get("VM_Name", "my")); 

Here we pass the qualified name as an actual XName and not as a colon-delimited string as it originates. Pay attention to the order; the namespace comes second using this syntax.

Now, once you have done all this, the other line in which you have a problem is:

string test = xml.ToString(); 

Here, xml refers to your root XML node whereas what you actually want is, presumably, the element for which you just queried: xml.Element(XName.Get("VM_Name", "my")). Furthermore, to get the text contents of that node, you should use the Value property. I suspect what you really want is:

string test = name.Value;
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • I replace the code to private void button1_Click(object sender, EventArgs e) { XElement xml = XElement.Load("C:\\Windows 7.xml"); IEnumerable propertyIDs = xml.Descendants("PropertyId"); foreach(XElement propertyID in propertyIDs) { XElement name = xml.Element(XName.Get("VM_Name", "my")); string test = name.Value; Console.WriteLine(test); } } – dwyane Jul 06 '11 at 03:57
  • I am getting the error NullReferenceException under String test = name.Value; – dwyane Jul 06 '11 at 03:59
  • @dwayne, the `Element` method will return `null` if the element is not found. Please *edit* your question and include the contents of `C:\\Windows 7.xml`. – Kirk Woll Jul 06 '11 at 04:01