0

i made a program where i retrieve xml code from my database SQL 2005, now i want to display all attribues along with thier values in windows form application. is there any function supports that?? and how?

<Permission>
    <CP name="Student">
        <tab name="studentinfo">
        </tab>
        <tab name="notes">
            <groupbox name="ss">
            <field type="textArea" x="xxx" />
            </groupbox>
        </tab>
    </CP>
    <CP name="Teacher">
    </CP>
    <CP name="doctor">
    </CP>
</Permission>

output: name="Student" name="Student info"

and so on..

3
  • do you have an example schema? Commented Nov 21, 2010 at 10:08
  • xml code isnt apearing here but u can get an idea Commented Nov 21, 2010 at 10:13
  • 2
    there's a code option to show code / xml etc... Its the button with 1010101 on it. I've fixed for you Commented Nov 21, 2010 at 10:15

2 Answers 2

1

You can do this by XML to Linq as bellow:

        XDocument xmlDoc = XDocument.Load("a.xml");
        var nodeAttrs = xmlDoc.Descendants().Select(x => x.Attributes());
        foreach (var attrs in nodeAttrs)
        {
            foreach (var attr in attrs)
                Console.WriteLine("Name: {0}, Value :{1}", attr.Name ,attr.Value);
        }

output is as bellow for your XML:

Name: name, Value :Student
Name: name, Value :studentinfo
Name: name, Value :notes
Name: name, Value :ss
Name: type, Value :textArea
Name: x, Value :xxx
Name: name, Value :Teacher
Name: name, Value :doctor

Edit: And if you have a string which represents your XML you can do

    var xmlString = "<Permission> <CP name=\"Student\"> <tab name=\"studentinfo\"></tab><tab name=\"notes\"><groupbox name=\"ss\"><field type=\"textArea\" x=\"xxx\" /></groupbox></tab></CP><CP name=\"Teacher\"></CP><CP name=\"doctor\"></CP></Permission>";
    byte[] byteArray = Encoding.ASCII.GetBytes( xmlString );
    MemoryStream stream = new MemoryStream( byteArray);

and then

var xmlDoc = XDocument.Load(stream);
Sign up to request clarification or add additional context in comments.

Comments

0

The XML to LINQ libraries make this pretty easy

  using (XmlTextReader reader = new XmlTextReader("C:/whatever.xml"))
  {
    reader.Read();
    XElement permission = (XElement)XElement.ReadFrom(reader);
    string name = permission.Element("CP").Attribute("name").Value;
    foreach (var tab in permission.Element("CP").Elements("tab"))
    {
      string tabName = tab.Attribute("name").Value;
    }
  }

1 Comment

can u display it in windows form? also i have the xml from database not a seperate file so how can i do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.