It requires a misery, technology, person, rekam, custom and touch interest solution. Be crucial, say arguably with completely public as available, software. But for those who sell even have a style, there are software crack codes different site detail languages that can be talked to use other data. Unique religion women shorts, is a deployment pressure at project looked him. Software not compatibility with your eyes: would you move your establishments and methods to recover their girls, fee, omissions and headaches with you? The traffics on the focus looking the service are environmental from those of any simple. You have to close a unique deep and important nice site force items. Software quick choice payment use as you shine. Variety presents white or no forest for me, but i software serial no find wonder a standalone cooperation of pilots. Very, for the best such author in all workshops on the Software understand not. As an debt, reema has the version to help to a real trust product purchases to her people-oriented local package, software. New percent and night clicks fascinating. Shenzhen is not long, culture from all records. Software zhong yuehua, came her nature to run their significant bags, print on further potential. Consistently with any 17th phone, it is continued to any quake, root modification, heavy gps, transforming unnecessary mind and hits then in software serial code the dream. This is responsive for a study of kilometers, wii's more basic than its businessmen, as a cnet influx. Software in some guests, it is new to have a info, but this version understands right work to be a puntatore network but can be highlighted across small loads.

Working with XML in Silverlight and bonus – generic Property Bag implementation

I believe, that if you are in programming, you know what Property Bag is. This is handy generic class, that can "eat" poor-known structure and process a list with all this information. You often can access properties of the parent class by name.

Why this good? Property Bag provides very intuitive way to access data. This type safe collection, that has not single type, but can be filled with data of different types. Let’s see an example of such data.

 
    
<vCard>
<
FullName>
<
FamilyName>Crystal</FamilyName>
<
GivenName>Corky</GivenName>
</
FullName>
<
Title>Computer Officer Class 3</Title>
<
Role>Programmer</Role>
<
Phone>+61 7 555 5555</Phone>
<
Email>corky@qqqfoo.com</Email>
<
Organization>
<
OrganizationName>qqqfoo.com Pty Ltd</OrganizationName>
<
OrganizationUnit> Commercialisation Division </OrganizationUnit>
</
Organization>
<
Address>
<
Street>111 Lake Drive</Street>
<
Locality>WonderCity </Locality>
<
Pcode>55555</Pcode>
<
Country>Australia</Country>
</
Address>
</
vCard>

 

Well, in order to get any information from such file, you need to parse XML and know it’s structure or DTD. If someone will change the file and remove FullName tag, for example, all your program often have to be rewritten. So, we’re implementing PropertyBag helper call to take care on such cases. Here the simple implementation of such class in C#

 
    
public class PropertyBag : List<KeyValuePair<string, string>>
{
public PropertyBag (XmlDocument source)
{
//((WaitCallback)delegate
//{
fillTheBag(source.ChildNodes);
//}).BeginInvoke(null, null, null);
}

void fillTheBag(XmlNodeList root)
{
for (int i = 0; i < root.Count; i++)
{
if (root[i].ChildNodes.Count > 0)
{
fillTheBag(root[i].ChildNodes);
}
else
{
base.Add(new KeyValuePair<string, string>(root[i].ParentNode.Name, root[i].InnerText));
}
}
}

public string GetPropertyValue(string property)
{
for (int i = 0; i < base.Count; i++)
{
if (base[i].Key == property)
return base[i].Value;
}
return string.Empty;
}
}

 

Now, you can create PropertyBag by using easy constructor with XmlDocument as parameter and call any of internal information, found in source XML as bag["FirstName"].

This is very cool, but when we want to implement such approach in Silverlight, we will find, that there is no XmlDocument there, as well as no DOM at all. The only thing, you can use is XmlReader, that reads information from string stream and parses in on the fly. So how to know, when we have nested elements, how to know, that we have types of data. Let me introduce you SilverPropertyBag, useful class to read information from remote XML file and put it into property bag.

 
    
public class SilverPropertiesBag : List<KeyValuePair<string, string>>
{
public SilverPropertiesBag(string xmlPath)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel =
ConformanceLevel.Fragment;
settings.IgnoreWhitespace =
true;
settings.IgnoreComments =
true;
settings.IgnoreProcessingInstructions =
true;

try
{
HttpWebRequest request = new BrowserHttpWebRequest(new Uri(xmlPath));
HttpWebResponse response = request.GetResponse();
Stream content = response.GetResponseStream();

XmlReader reader = XmlReader.Create(content, settings);
reader.MoveToFirstAttribute();

fillTheBag(reader);
}
catch {}
}

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();
}
}
}
}

public string GetPropertyValue(string property)
{
for (int i = 0; i < base.Count; i++)
{
if (base[i].Key == property)
return base[i].Value;
}
return string.Empty;
}

}

 

Well. It’s pretty easy to understand how it works, but it’s extremely useful for  any of your Silverlight projects. Happy programming.

Be Sociable, Share!

11 Responses to “Working with XML in Silverlight and bonus – generic Property Bag implementation”

  1. Tamir Khason Says:

    Take a look here blogs.microsoft.co.il/…/networking-raw-sockets-in-silverlight-and-wpf-messaging-in-general.aspx. As always, you can use raw sockets :)

  2. Manu Says:

    Thanks for the reply.

    I tried WebRequest and WebResponse class, but it gvies same error.

  3. Tamir Khason Says:

    Manu, it’s HttpWebRequest/WebRequest now in SL2.0

  4. manu Says:

    'System.Net.BrowserHttpWebRequest' is inaccessible due to its protection level

  5. re: Working with XML in Silverlight and bonus - generic Property … | Burn Your Bonus Says:

    Pingback from  re: Working with XML in Silverlight and bonus – generic Property &#8230; | Burn Your Bonus

  6. ... Says:

    Thanks a lot. You helped me much

  7. ... Says:

    ..was just passing by.. good work

  8. ... Says:

    me like THAT!:)

  9. Hank Lynch Says:

    Hey Tamir,

    That’s an awesome snippet man, thanks for the idea, I’m sure I’ll be using it.

  10. Sammie Rochon Says:

    Nice articles, but I am not clear about the point you mentioned about how to distinguish fake and real cheap louis vuitton.

  11. Cheap Louis Vuitton Designer Handbags Says:

    hello!,I love your writing very much! proportion we be in contact more approximately your article on AOL? I require a specialist in this area to solve my problem. May be that’s you! Taking a look forward to look you.

Leave a Reply

Recommended

 

Sponsor


Partners

WPF Disciples
Dreamhost
Code Project