简体   繁体   中英

XML - C# Generic method for getting a Value using a Key on a Serialized Object

I have an xml file that looks like below. And I want to create a method that would get me the value using the key.

    </cbn:Class>
       <cbt:Students>
          <cbt:Student>
            <cbt:Key>x-param-key-studentName-1</cbt:Key>
            <cbt:Value>x-param-studentSurname-val</cbt:Value>
          </cbt:Student>
          <cbt:Student>
            <cbt:Key>x-param-key-studentName-2</cbt:Key>
            <cbt:Value>x-param- studentSurname-val</cbt:Value>
          </cbt:Student>
        </cbt:Students>
    </cbn:Class>

So, i want to get the valud of studentSurname using as key the studentName . The xml is serialized, so I can access for example the Student as an object. I can get the value of the first student by using the code I have written as below:

string studentSurname = myData.Class.Students.Student[0].Value;

Where myData is the serialized object. I would like a more generic method instead of getting just the first student.
EDIT: Another XML

<?xml version="1.0" encoding="UTF-8"?>
<cbn:PaidOrderNotification xmlns:cbn="http://*/3.12.0.0/*.xsd">
    <cbn:NotificationDate>2016-07-29T11:59:29.1137865Z</cbn:NotificationDate>
    <cbn:Purchase cbt:Id="95233035" xmlns:cbt="http://xml.*.com/3.12.0.0/*.xsd">
        <cbt:Status>Test Order</cbt:Status>
        <cbt:StatusId>TST</cbt:StatusId>
        <cbt:Items>
            <cbt:Item cbt:RunningNo="1">
                <cbt:ProductId>175358</cbt:ProductId>
                <cbt:ProductReportingGroup>Basic alle Laufzeiten</cbt:ProductReportingGroup>
                <cbt:YourCurrencyId>EUR</cbt:YourCurrencyId>
                <cbt:ProfitCalculation>
                    <cbt:GrossRevenue>566.44</cbt:GrossRevenue>
                </cbt:ProfitCalculation>
                <cbt:YourPrice>
                    <cbt:TotalTotalPrice>
                    </cbt:TotalTotalPrice>
                </cbt:YourPrice>
                <cbt:Deliveries />
                <cbt:Additionals />
                <cbt:ExtraParameters />
            </cbt:Item>
        </cbt:Items>
        <cbt:ExtraParameters>
            <cbt:ExtraParameter>
                <cbt:Key>x-my-key</cbt:Key>
                <cbt:Value> x-my-val</cbt:Value>
            </cbt:ExtraParameter>
        </cbt:ExtraParameters>
    </cbn:Purchase>
</cbn:PaidOrderNotification>

I would like to take from here this value:

<cbt:ExtraParameters>
            <cbt:ExtraParameter>
                <cbt:Key>x-didi</cbt:Key>
                <cbt:Value>sfsfd</cbt:Value>
            </cbt:ExtraParameter>
        </cbt:ExtraParameters>

How can i do that?

Your XML document have namespaces but your example do not includes it, do I had to fake some values.

Once you have the URI for the namespaces, you can use a XmlNamespaceManager to look into the document with the appropriate qualifier

var xml = @"
    <cbn:Class xmlns:cbn='something' xmlns:cbt='something-else'>
        <cbt:StudentNr></cbt:StudentNr>
        <cbt:Students>
            <cbt:Student>
                <cbt:Key>x-param-key-studentName-1</cbt:Key>
                <cbt:Value>x-param-studentSurname-val</cbt:Value>
            </cbt:Student>
            <cbt:Student>
                <cbt:Key>x-param-key-studentName-2</cbt:Key>
                <cbt:Value>x-param- studentSurname-val</cbt:Value>
            </cbt:Student>
        </cbt:Students>
    </cbn:Class>";

var doc = new XmlDocument();
    doc.LoadXml(xml);

var students = doc.SelectSingleNode("//*[local-name() = 'Students']");
var nsmgr = new XmlNamespaceManager(doc.NameTable);
    nsmgr.AddNamespace("cbt", students.NamespaceURI);

var dictionary =  students.SelectNodes("cbt:Student", nsmgr).OfType<XmlElement>()
    .ToDictionary(student => student.SelectSingleNode("cbt:Key"  , nsmgr).InnerText,
                  student => student.SelectSingleNode("cbt:Value", nsmgr).InnerText);

EDIT: You can do this as well:

string studentSurname = "";
var dictionary = myData.Class.Students.ToDictionary(student => student.Key, 
                                                    student => student.Value);
var exists = dictionary.TryGetValue("x-param-key-studentName-1", out studentSurname);

EDIT2: Note you have two different cbt:ExtraParameters elements, and the first one is empty. I extended the code to work with any cbt:ExtraParameter elements

var doc = new XmlDocument();
    doc.LoadXml(xml);

var dictionary = doc.SelectNodes("//*[local-name() = 'ExtraParameter']")
    .OfType<XmlElement>()
    .ToDictionary(
        extra => extra.SelectSingleNode("*[local-name() = 'Key'  ]").InnerText,
        extra => extra.SelectSingleNode("*[local-name() = 'Value']").InnerText
    );

var studentSurname = "";
var exists = dictionary.TryGetValue("x-my-key", out studentSurname);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM