Needs space in the element name in xml formation - c#

I've set of data in the database and needs to convert to the xml format, but the problem is one of the element name has the space between the element name but I want to use this name in the xml
<Data>
<Out put xml>
<ROW>
</ROW>
</Out put xml>
<Data>
I aware about that couldn't possible to use space in the element name. Please suggest me any other alternative to achieve this.

Try to encode the name while converting to xml
XmlConvert.EncodeName(Name);
Then to convert back just decode it
XmlConvert.DecodeName(Name);

Related

Create xml file in binary format in C#

I want to create a xml file which has xml declaration, root node and child nodes.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<Tag1>
<SubTag>
<Id>
</Id>
<Name>IdentityManagement</Name>
<Time>4/11/2017 6:26:15 PM</Time>
<Message>Message1</Message>
</SubTag>
<SubTag>
<Id>
</Id>
<Name>MainWindow</Name>
<Time>4/11/2017 6:26:20 PM</Time>
<Message>Message2</Message>
</SubTag>
</Tag1>
But I need to write this xml in binary format, so no one can read it.
On calling of one function, one can add another SubTag.
So there can be n number of .
If you want to convert it into a form that is not trivially readable by a human, encode it to base64:
Convert.ToBase64String(textAsBytes);
If it should not be readable by anyone under any circumstances, encrypt it.
I am not sure what you mean when you say 'binary' though, all text is already binary when stored in a file, it is just encoded using an encoding scheme like ASCII or UTF8.

Remove XML that does not validate against XSD

I have an XML and XSD.
The problem I have is that if one element\attribute fails during the upload then nothing is uploaded. Therefore using the XSD, I would like to strip out any invalid “rows” prior to the upload.
If the following is taken as example
<Row>
<Column1>1</Column1>
<Column2>2</Column2>
</Row>
<Row>
<Column1>1</Column1>
<Column2>2</Column2>
</Row>
<Row>
<Column1>1</Column1>
**<Column2>**B**</Column2>**
</Row>
<Row>
<Column1>1</Column1>
**<Column2>**C**</Column2>**
</Row>
In the above example, Column2 in the 3rd Row and 4th row is invalid. Therefore I would like to remove it both from the XML.
I tried
foreach (XmlElement row in doc.SelectNodes("TableName/Row"))
{
if (row.SchemaInfo.Validity == XmlSchemaValidity.Invalid)
{
row.ParentNode.RemoveChild(row);
}
}
but it removes only the first error section and if later there are sections with error the SchemaInfo.Validity value is "NotKnown"
I think the only way to do this would be to manually validate the XML using your own code.
Due to the possible structure of an XSD and the possible errors that could occur in it, creating a validator that can consistently skip over an error and continue, would be very difficult (and hence is not something that any of the parsers i'm aware of have done).
In some circumstances they will continue validation after an error, but typically they then ignore all siblings after the initial error (in order to get back to a more consistent state). Basically once an error is encountered there are often multiple validation paths that can be taken as the validation state has become ambiguous.
That said if your data is something along the lines of your sample and you have some control over your XSD you could refactor the XSD defintion of <row> to be root element (then use an element ref where you need it). You could then load each <row> element one at a time and validate each one as you go. That way the code that reads the document is disconnected from the validation of each <row>, so if one is invalid you discard it and move onto the next.
NOTE : This approach would mean the rest of the XML document is NOT validated.

Extracting Xml Elements from Outerxml type

Probably another easy one but I cant find the answer and cant work it out.
I have a outerxml packet for example
<field1 id="abc" passed="False">
<field2 id="BCD" reason="yada"/>
</field1>
in asp.net (c#) how can I retrieve the value of "reason"?
You can use LINQ to XML:
string reason = (string)xml.Element("field1").Element("field2").Attribute("reason");

Add attribute without name and value?

Whether XML element attribute name can be empty:
<?xml version="1.0" encoding="utf-8"?>
<test>
<tables>
<gg qqq="">
<ss ""=""/>
<mm ""=""/>
</gg>
</tables>
</test>
I am getting an exception I cant to load this. Is this possible?
No, this will produce invalid XML. Attribute must have both name and value. How do you image opposite?
No this can not be done.
XML has empty element concept[If you are trying to implement that].
In that whole element is kept empty and said to be null as follow>>
<abc></abc>
<DefaultTaxPrice></DefaultTaxPrice>
Short answer, no. If you want to add value but not know what is the name of the value in XML, insert in into the body instead.
<ss>value</ss>
This way, your ss will be treated as a value

Why doesn't XDocument.Parse() parse my XML properly?

I am trying to use XDocument.Parse(string s) to parse some XML that is being returned from a REST based API. After the XML is parsed, it creates a new XDocument, but the document doesn't contain the properly parsed XML nodes. The name of the first node is the correct node name, but the value is the the concatenation of all the text from the XML, regardless of which Element is belongs to. Can anybody help me figure out what is going on?
XML
<sci_reply version="1.0">
<send_message>
<device id="00000000-00000000-00000000-00000000">
<error id="303">
<desc>Invalid target. Device not found.</desc>
</error>
</device>
<error>Invalid SCI request. No valid targets found.</error>
</send_message>
</sci_reply>
Debug View of XDocument Object
That's the expected behavior. The Value of a an XML element is concatenation of values of all its children. If you want to actually access the XML, read something about LINQ to XML or classes in the System.Xml.Linq namespace.
Thats just the debugger being nice.
The root is being displayed with all of its children.

Categories