0

In C#, I have an xml string named inputXmlString like this:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dd="clr-namespace:DiagramDesigner;assembly=GUI">
              <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                  <LinearGradientBrush.GradientStops>
                    <GradientStop Color="#FFFAFBE9" Offset="0" />
                    <GradientStop Color="#FF00FFFF" Offset="1" />
                  </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
              </Setter.Value>
<TextBox Background="#00FFFFFF" Foreground="#FF000000" HorizontalAlignment="Center" VerticalAlignment="Center">Thread</TextBox>
</Grid>

Now I want to get the text of the TextBox element. For that I tried this method:

XElement inputElement = XElement.Parse(inputXmlString);
XElement textbox = inputElement.Element("TextBox");
string result = textbox.Value;

But the textbox element comes as null here. Any suggestion?

1 Answer 1

3

You have a namespace on your element, you need to specify it:

XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XElement textbox = inputElement.Element(ns + "TextBox");
Sign up to request clarification or add additional context in comments.

Comments

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.