Skip to main content

Microsoft Silverlight

Answered Question XMLDocument,XMLNodeList in SilverlightRSS Feed

(0)

petit_a
petit_a

Member

Member

2 points

6 Posts

XMLDocument,XMLNodeList in Silverlight

Hi, Can anyone tell me what I can write instead of XMLDocument and XMLNodeList in Silverlight?I've read forums and found that I should use XDocument which is from system.linq, but it is not working.I get the error below The type or namespace "XDocument" can not be found(are you missing a using directive or an assembly reference?) I'm trying to write a parser and have never done it before so I'm having a hard time.Can someone please give me a simple instruction? Thanks

Bill Reiss
Bill Reiss

Contributor

Contributor

4840 points

919 Posts

Silverlight MVP

Re: XMLDocument,XMLNodeList in Silverlight

You need to add a reference to the System.Xml.Linq.dll in your Silverlight application project. Once you do this you should be able to use XDocument, etc.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

petit_a
petit_a

Member

Member

2 points

6 Posts

Re: XMLDocument,XMLNodeList in Silverlight

Thank you Bill.Now I can use XDocument but I can't use XmlNodeList or XDocument.readnode(),... Here is what I'm trying to write XmlNodeList configs = xmlDocument.ReadNode(XmlReader.Create("config.xml")).ChildNodes; Can you help me with that?Thanks

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: XMLDocument,XMLNodeList in Silverlight

Hi Petit_a,

petit_a:
Here is what I'm trying to write XmlNodeList configs = xmlDocument.ReadNode(XmlReader.Create("config.xml")).ChildNodes;

Please take a look at this article.   A sample provides there.

public PropertyBag (XmlDocument source)       

//((WaitCallback)delegate     
//{ 

fillTheBag(source.ChildNodes);
     
//}).BeginInvoke(null, null, null);       
}

 

void fillTheBag(XmlReader root) 

   
int depth = root.Depth;     
    
KeyValuePair<string, string> last = new KeyValuePair<string, string>(); 
  
while (root.Read())    
   {     
       
switch (root.NodeType)   
       { 
            
case XmlNodeType.Text: 
            last =
new KeyValuePair<string, string>(root.Name, root.Value); 
            
break;
     
             
case XmlNodeType.EndElement: 
            
if (last.Key == string.Empty && last.Value != string.Empty)  { 
              last =
new KeyValuePair<string, string>(root.Name, last.Value); 
              
base.Add(last); 
              last =
new KeyValuePair<string,string>();
     
          } 

         
if (depth > root.Depth)  return
         
break
      
 

      }

     
if (root.IsEmptyElement || root.EOF) return;     
else if (root.IsStartElement())     

string name = root.Name;     
if (root.HasAttributes) 
{

 while (root.MoveToNextAttribute())       

base.Add(new KeyValuePair<string, string>(root.Name, root.Value));     

root.Read();     
if (root.NodeType == XmlNodeType.CDATA && root.Value.Length == 0)     
{   
root.Read(); 

if (root.NodeType == XmlNodeType.Text && root.ValueType == typeof(string) && root.Value != string.Empty)     

base.Add(new KeyValuePair<string, string>(name, root.Value));     

if (root.NodeType == XmlNodeType.EndElement && root.Name.Equals(name))    

root.Read();     
if (root.Depth < depth) 
return
else 
continue;    

      
if (root.IsStartElement())     

      
if (root.Depth > depth)     
fillTheBag(root); 
else 
continue


root.Read();       


      

    

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

juliako
juliako

Member

Member

28 points

14 Posts

Re: XMLDocument,XMLNodeList in Silverlight

Hi, you might find the following documentation helpful: http://msdn.microsoft.com/en-us/library/cc188996(VS.95).aspx.

I am the programming writer on the .Net and Silverlight .Net Managed XML team. If you have questions, you can send me an email at juliako@microsoft.com.

Thank you,

Julia

Julia Kornich [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities