开发者

C#, XML typed column: How to read /set values?

开发者 https://www.devze.com 2023-03-19 03:17 出处:网络
I am a XML newbie and need some help to get back on track. I have the following xsd code where I have these two xsd elements:

I am a XML newbie and need some help to get back on track.

I have the following xsd code where I have these two xsd elements:

...
<xsd:element name="DataCollection">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Data" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Question" minOccurs="0" type="xsd:string" />
                            <xsd:element name="Input" minOccurs="0" type="xsd:string" />
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
...

This xsd schema is assigned to a DB column of t开发者_如何学运维ype XML.

Lets say the table is called "A" (in my C# code)

while the XML typed column is called "XML_B".

The entity framework is used for DB access.

The newbie questions: I think that I can read this way:

string test = A.DataCollection.Data.FirstOrDefault(row => row.Input == "Hello world?").Question;

This would give me the first (or default) DB entry/value of field "Question" where the XML field "Input" contains exactly "Hello world?".

But unfortunetally raises the read try an error like "Object not set to an instance" because "...Data" is equal to null and this doesnt solve it at all:

...
if (A.DataCollection.Data == null)
     A.DataCollection.Data = new DataCollectionData[1];
...

And how can I write values for "Question" and "Input"?

A.DataCollection.Data.SetValue("Test value for field 'Question'", 0);   // Index 0 = XML field "Question"
A.DataCollection.Data.SetValue("Test value for field 'Input'", 1);      // Index 1 = XML field "Input"

Any help would be really great.

Thanks in advance!

0

精彩评论

暂无评论...
验证码 换一张
取 消