reading XML
-
i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!
Didn't you post this same question two days ago?
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
Didn't you post this same question two days ago?
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.no my last question was using xpath, and not making any progress.... I guess I will stop posting on code project... I'm not asking someone to write this for me, I just have something new and is not working was trying to get someone to help point out what i am doing wrong so i can move forward. sorry for reposting a different but similar question.
-
no my last question was using xpath, and not making any progress.... I guess I will stop posting on code project... I'm not asking someone to write this for me, I just have something new and is not working was trying to get someone to help point out what i am doing wrong so i can move forward. sorry for reposting a different but similar question.
No, it is just working with those tools is just an absolute PITA and to help most of us would have to paste it into a project and fiddle unless the error just jumps out at us. I usually just use xsd.exe to create a class from the XML file and then serialize into the class rather than xpath since it is lazier and easier in .NET.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!
Hi again, The xml was missing some end tags so I added them to test this case. When I added a name alias on xmlns attributes it began to work. Here's the code I used. Note the aliases!
string testText = "<?xml version=\"1.0\" encoding=\"utf-16\" ?> <GPO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:**Alias1**=\"http://www.microsoft.com/GroupPolicy/Settings\"> <Identifier> <Identifier xmlns:**Alias2**=\"http://www.microsoft.com/GroupPolicy/Types\">{89AEAFFE-E1F8-4786-8AF7-6BE2D991625E}</Identifier></Identifier></GPO>"; System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.LoadXml(testText); System.Xml.XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); string Identifier = xmln.InnerText.ToString();
Hope this helps you forward, MikaThe need to optimize rises from a bad design. My articles[^]
-
No, it is just working with those tools is just an absolute PITA and to help most of us would have to paste it into a project and fiddle unless the error just jumps out at us. I usually just use xsd.exe to create a class from the XML file and then serialize into the class rather than xpath since it is lazier and easier in .NET.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.i've never used xsd.exe before this project but when i tried to create my own xsd files from the xml it complained about to many nested tables. when i found the MS xsd files i tried to create classes but it is saying something about undefined complexType. if there is a way I can use the MS xsd files then great if not i need to figure out how to parse this xml file the hard way and i know it is going to be a PITA.
-
i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!
-
i am trying to read from an xml document but am not having any luck getting selectsinglenode to work here is the code i am using XmlDocument xml = new XmlDocument(); xml.Load(str); //str has the path to my XML doc XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); //xmln is always null but xml has my xmldoc loaded in memory string Identifier = xmln.InnerText.ToString(); //code bombs here cuz xmln is null... here is the first few lines of the xml - - {89AEAFFE-E1F8-4786-8AF7-6BE2D991625E} i have 20 XSD schema files that are related to the xml docs i am trying to parse but i'm new to xml and not clear on how to use the xsd's help in either direction would be great!
add http://www.microsoft.com/GroupPolicy/Settings to a new xml namespace manager and use it with your node select. Something like: XmlNamespaceManager NSM = new XmlNamespaceManager(YOURXMLDOCOBJINSTANCE.NameTable); NSM.AddNamespace("some_random_name_like_abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode whateverNode = YOURXMLDOCOBJINSTANCE.SelectSingleNode("//some_random_name_like_abcd:WhateverElement", NSM); XPath will generally not work without specifying the namespace, when a namespace is defined.
-
Hi again, The xml was missing some end tags so I added them to test this case. When I added a name alias on xmlns attributes it began to work. Here's the code I used. Note the aliases!
string testText = "<?xml version=\"1.0\" encoding=\"utf-16\" ?> <GPO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:**Alias1**=\"http://www.microsoft.com/GroupPolicy/Settings\"> <Identifier> <Identifier xmlns:**Alias2**=\"http://www.microsoft.com/GroupPolicy/Types\">{89AEAFFE-E1F8-4786-8AF7-6BE2D991625E}</Identifier></Identifier></GPO>"; System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.LoadXml(testText); System.Xml.XmlNode xmln = xml.SelectSingleNode("/GPO/Identifier/Identifier"); string Identifier = xmln.InnerText.ToString();
Hope this helps you forward, MikaThe need to optimize rises from a bad design. My articles[^]
-
thanks for the tip but the xml code i don't want to change it is an export that i want to import to a database
Ok, then I think you should use namespace managers. Look at the second override of this method:
public XmlNode SelectSingleNode(
string xpath,
XmlNamespaceManager nsmgr
)http://msdn.microsoft.com/en-us/library/h0hw012b.aspx[^]
The need to optimize rises from a bad design. My articles[^]
-
add http://www.microsoft.com/GroupPolicy/Settings to a new xml namespace manager and use it with your node select. Something like: XmlNamespaceManager NSM = new XmlNamespaceManager(YOURXMLDOCOBJINSTANCE.NameTable); NSM.AddNamespace("some_random_name_like_abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode whateverNode = YOURXMLDOCOBJINSTANCE.SelectSingleNode("//some_random_name_like_abcd:WhateverElement", NSM); XPath will generally not work without specifying the namespace, when a namespace is defined.
thanks, i just tried this and making some progress now here is what i have to get the text of the whole node and the first child XmlDocument xml = new XmlDocument(); xml.Load(str); XmlNamespaceManager NSM = new XmlNamespaceManager(xml.NameTable); NSM.AddNamespace("abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode xmln = xml.SelectSingleNode("//abcd:Identifier", NSM); if (xmln.HasChildNodes == true) { if (xmln.FirstChild.Name == "Identifier") { this.textBox2.Text = xmln.FirstChild.InnerText.ToString(); } } this.textBox1.Text = xmln.InnerText.ToString(); is there a better way to do this or should i get coding? and thanks to everyone!!!
modified on Thursday, November 13, 2008 4:15 PM
-
thanks, i just tried this and making some progress now here is what i have to get the text of the whole node and the first child XmlDocument xml = new XmlDocument(); xml.Load(str); XmlNamespaceManager NSM = new XmlNamespaceManager(xml.NameTable); NSM.AddNamespace("abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode xmln = xml.SelectSingleNode("//abcd:Identifier", NSM); if (xmln.HasChildNodes == true) { if (xmln.FirstChild.Name == "Identifier") { this.textBox2.Text = xmln.FirstChild.InnerText.ToString(); } } this.textBox1.Text = xmln.InnerText.ToString(); is there a better way to do this or should i get coding? and thanks to everyone!!!
modified on Thursday, November 13, 2008 4:15 PM
rather use /abcd:Identifier/abcd:Identifier to get only the child nodes labeled "Identifier" of the root "Identifier" node. Use the full power of XPath if you went that way ... w3 has a great tutorial on how to use them "productive" ... http://www.w3schools.com/XPath/xpath_syntax.asp[^]
Code? Yeah i love it fried together with a glass of wine.
-
rather use /abcd:Identifier/abcd:Identifier to get only the child nodes labeled "Identifier" of the root "Identifier" node. Use the full power of XPath if you went that way ... w3 has a great tutorial on how to use them "productive" ... http://www.w3schools.com/XPath/xpath_syntax.asp[^]
Code? Yeah i love it fried together with a glass of wine.
thanks for the link it has been helpful, I have another question here is the xml i am trying to query ... .....settings I need to read are child nodes here..... Folder Redirection .....settings I need to read are child nodes here..... Registry Is there a way i can query the xsi:type value? if not I can query the name but not sure what the best way to jump up to the settings to read them. this xml file does not always have the same entries so i am checking for all possible cases
-
thanks for the link it has been helpful, I have another question here is the xml i am trying to query ... .....settings I need to read are child nodes here..... Folder Redirection .....settings I need to read are child nodes here..... Registry Is there a way i can query the xsi:type value? if not I can query the name but not sure what the best way to jump up to the settings to read them. this xml file does not always have the same entries so i am checking for all possible cases
i figured out how to do this below is the code i used, could be cleaner if this was the only thing i was checking in the User node XmlDocument xml = new XmlDocument(); xml.Load(str); XmlNamespaceManager NSM = new XmlNamespaceManager(xml.NameTable); NSM.AddNamespace("abcd", "http://www.microsoft.com/GroupPolicy/Settings"); XmlNode xmln6 = xml.SelectSingleNode("//abcd:User", NSM); for (int i = 0; i < xmln6.ChildNodes.Count; i++) { XmlNode Childnode = xmln6.ChildNodes[i]; if (Childnode.Name == "ExtensionData") { string ExtDataTest = Childnode.ChildNodes[0].Attributes[1].Value.ToString(); string test = ExtDataTest.Substring(3, ExtDataTest.Length - 3) } }