开发者

Extract contents from XML file

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.Elem开发者_如何学Cents())
    {
        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


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜